Benny G
Benny G

Reputation: 101

No update or numberOfRowsInSection after delete with commitEditingStyle

My table doesn't update after removing an item with Edit / Delete or the swipe-delete gesture. If I do Edit / Delete, the Delete button doesn't disappear. It stays there in a pressed/stuck state. When I then click Done, the Delete button goes away. A screenshot of this is attached at the bottom of this post.

My numberOfRowsInSection code is executed when the app is started, but not after the deletion of an item.

I'm using a UITableViewController. The definition in my .h file:

@interface BNVFavoritesTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>{    
  BNVAppDelegate *appDelegate;    
  IBOutlet UITableView *myTable;
}

@property (retain, nonatomic) IBOutlet UITableView *myTable;
@end

Here's the relevant code from my .m file:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"number of rows:\t%d", [appDelegate.favoritesArray count]);
    return [appDelegate.favoritesArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    //Get the object from the array.
    BNVFavorite *favoriteObj = [appDelegate.favoritesArray objectAtIndex:indexPath.row];

    //Set the name.
    cell.textLabel.text = favoriteObj.name;

    // Set up the cell
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete){
        NSLog(@"before delete\t%d", [appDelegate.favoritesArray count]);        
        BNVFavorite *selectedObject = [appDelegate.favoritesArray objectAtIndex:indexPath.row];
        NSString *selectedName =  selectedObject.name;
        // delete from sqlite database
        [appDelegate removeFavoriteFromDB:selectedName];
        // delete from memory array
        [appDelegate.favoritesArray removeObjectAtIndex:indexPath.row];
        // delete in user interface
        [myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        NSLog(@"after delete\t%d", [appDelegate.favoritesArray count]);

    }

}

When deleting, I see 'before delete' and 'after delete' messages in my log (as expected), but no 'number of rows' messages.

My datasource and delegate outlets from the Table View are connected to the TableViewController class. I also tried doing this in the ViewDidLoad method, but that didn't make a diferent. Forcing a [myTable reloadData]; at the end of my commitEditingStyle is also not helping.

Finally, here's a screenshot of that 'stuck' delete button. https://i.sstatic.net/ukUu0.png

Clicking it a few times causes an NSRangeException/out of bounds error, indicating that the code from commitEditingStyle is executed.

Upvotes: 1

Views: 1257

Answers (3)

Rajneesh071
Rajneesh071

Reputation: 31091

Just check your connection in connection inspector for your table.

or if it is properly connected then Reload your table after deleting record

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete){
        NSLog(@"before delete\t%d", [appDelegate.favoritesArray count]);        
        BNVFavorite *selectedObject = [appDelegate.favoritesArray objectAtIndex:indexPath.row];
        NSString *selectedName =  selectedObject.name;
        // delete from sqlite database
        [appDelegate removeFavoriteFromDB:selectedName];
        // delete from memory array
        [appDelegate.favoritesArray removeObjectAtIndex:indexPath.row];
        // delete in user interface
        [myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        NSLog(@"after delete\t%d", [appDelegate.favoritesArray count]);
        [myTable reloadData];
    }

}

Upvotes: 0

Benny G
Benny G

Reputation: 101

After hours of searching, I found the problem. Turns out the referencing outlet from the tableview in the storyboard to myTable in my controller class didn't exist anymore. I must have removed it accidentally. I found out about it when I noticed the code started working when I replaced myTable with self.tableView.

Upvotes: 2

sunkehappy
sunkehappy

Reputation: 9101

You need to add your delete animation between beginUpdates and endUpdates.

[myTable beginUpdates];
[myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[myTable endUpdates];

Upvotes: 0

Related Questions