Dolomo KodoMo
Dolomo KodoMo

Reputation: 21

How to delete cell in tableview

Help me please. I was trying to delete cell in tableview. I am beginner in iOS development.

please help me.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    int num = [[self.fetchedResultsController sections] count];
    return [[self.fetchedResultsController sections] count]; 
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    id sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    NSInteger numberOfRows = [sectionInfo numberOfObjects];

    return numberOfRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath
{
      id cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
      [self configureCell:cell atIndexPath:indexPath];

     return cell;

}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
     if (editingStyle == UITableViewCellEditingStyleDelete) {
     // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
     } 
     else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
     }
}

method is connect class tableviewcell in my cell have more detail.

- (void)configureCell:(id)cell atIndexPath:(NSIndexPath *)indexPath
{
    BookDetail *bookDetail = [self.fetchedResultsController objectAtIndexPath:indexPath];

    [(storageuser *)cell settingupWithInfo:bookDetail];

}

Upvotes: 0

Views: 154

Answers (3)

Christian Zanin
Christian Zanin

Reputation: 33

You have to implement the UITableViewDataSource method

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
      return YES;
}

In this way all the cell of your table view can be deleted. If you want to avoid removing cell for particular indexes path, simply put a condition inside.

Upvotes: 1

zbMax
zbMax

Reputation: 2818

In tableView:commitEditingStyle:forRowAtIndexPath:, you have to update your datasource (it seams to be self.fetchedResultsController) before calling deleteRowsAtIndexPaths:withRowAnimation:

Upvotes: 1

Antenehs
Antenehs

Reputation: 356

You should also have to remove the entry from your data source with the following

[self.managedObjectContext deleteObject:bookDetail];
[self.managedObjectContext save:nil]; 

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

Upvotes: 0

Related Questions