Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete row from tableView

I am a bit lost on what should be a simple action of deleting a row from a tableview. I have an sqlite database with a table called SUBJECT and I want to delete a record for this table, delete the member from the array and delete the entry in the tableview. I don't know what sequence is best and I keep getting errors.

Here is the code:

- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if(editingStyle == UITableViewCellEditingStyleDelete) {

        QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate];

        //Get the object to delete from the array.
        Subject *sub = [self.subjects objectAtIndex:indexPath.row];

        // check if there are any quoteMaps for this subject and if so do not do it

        NSArray *qm = [appDelegate getQuoteMapsFromSubId:sub.subject_id];

        if (qm.count==0){

            //Delete the object from the table.
            [self.subjects removeObjectAtIndex:indexPath.row];
            [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [sub deleteSubject];


        } else {
            //DON'T DO IT BUT GIVE A WARNING INSTEAD

        }
    }
}

I get the following error on the self.subjects removeObject.... line:

-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x6b2a8f0
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 
'-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x6b2a8f0'

The error message says my self.subjects is an NSArray yet my header assigns it as NSMutableArray:

@interface SubjectViewController : UITableViewController <UIAlertViewDelegate>  {

    NSMutableArray *subjects;

}

@property (nonatomic, retain) NSMutableArray *subjects;

Why is it changing to NSArray???

I checked and found that the Parent ViewController is assigning the following:

NSPredicate *catFilter = [NSPredicate predicateWithFormat:@"category_title == %@", cat.category_title];

NSArray *subs = [appDelegate.subjects filteredArrayUsingPredicate:catFilter];
 subViewController.subjects = (NSMutableArray *) subs;

Looks like the problem is there.

I get the following error if I comment out the self.subjects removeObject and try to run the next line of "tv deleteRowsAtIndexPaths...":

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'Invalid update: invalid number of rows in section 0.  The number of rows
contained in an existing section after the update (1) must be equal to the number 
of rows contained in that section before the update (1), plus or minus the number 
of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or 
minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

So far this was narrowed down to being a problem in the prior controller view where I am using an NSArray out of a NSPredicate because I couldn't get NSPredicate to work with NSMutableArray. So now that is the issue trying to be resolved.

Or converting/assigning an NSArray as an NSMutableArray...

like image 314
jroyce Avatar asked Jul 12 '26 05:07

jroyce


2 Answers

NSArrays are inmmutable, that means you can't add or remove elements, hence your object doesn't have a removeObjectAtIndex: method.

  • Check your property to see if its an NSArray instead of an NSMutableArray.
  • Check that the object in that pointer is in fact an NSMutableArray.
  • To assign an NSArray to your variable first create an NSMutableArray from it using the arrayWithArray: method.
like image 87
whtlnv Avatar answered Jul 13 '26 19:07

whtlnv


first your subjects array is immutable. use NSMutableArray class for subject array.

also try to use beginUpdates - endUpdates block...

[tv beginUpdates];
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tv endUpdates];
like image 26
Alkimake Avatar answered Jul 13 '26 21:07

Alkimake



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!