简体   繁体   中英

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];

}

tableView:commitEditingStyle:forRowAtIndexPath: ,您必须在调用deleteRowsAtIndexPaths:withRowAnimation:之前更新数据源(其接缝为self.fetchedResultsControllerdeleteRowsAtIndexPaths:withRowAnimation:

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.

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM