簡體   English   中英

如何在TableView中刪除單元格

[英]How to delete cell in tableview

請幫幫我。 我試圖刪除表格視圖中的單元格。 我是iOS開發的初學者。

請幫我。

- (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
     }
}

方法是在我的單元格中連接類tableviewcell有更多詳細信息。

- (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:

您必須實現UITableViewDataSource方法

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

這樣,可以刪除表視圖的所有單元格。 如果要避免刪除特定索引路徑的單元格,只需在其中放置一個條件。

您還必須使用以下命令從數據源中刪除條目

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM