【问题标题】:crash application while delete section from UITableView从 UITableView 删除部分时崩溃应用程序
【发布时间】:2013-07-04 11:05:50
【问题描述】:

在tableview我有很多部分,在部分的第一行我有一个删除按钮,我想在点击按钮后删除部分但是它崩溃了请帮助我怎么做?这是代码...

//****** adding delete section button
     CellButton *_sectionButton = (CellButton*)[cell viewWithTag:10];
     _sectionButton.hidden = YES;
     _sectionButton.indexPath = indexPath;
     [_sectionButton addTarget:self action:@selector(sectionDeleteButton:)      forControlEvents:UIControlEventTouchUpInside];

-(void)sectionDeleteButton: (id)sender
{
   CellButton *_secbtn = (CellButton*)sender;
   NSInteger sectionNumber = _secbtn.indexPath.section;
   [self.tableView beginUpdates];
   [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionNumber] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
    [self.tableView reloadData];

}

编辑:错误信息是:

断言失败 -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:1054 2013-07-04 04:25:15.921 估计 [9025:c07] *** 由于应用程序终止未捕获的异常“NSInternalInconsistencyException”,原因:“无效更新:无效的节数。更新后表视图中包含的节数(2)必须等于更新前表视图中包含的节数(2),加上或减去插入或删除的节数(0插入,1已删除)

【问题讨论】:

  • 建议将错误信息添加到此类问题中。
  • 删除行后,在 numberOfScetionsInTableview 方法中更新您的节数。
  • @allprog 这个错误...断言失败 -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:1054 2013-07-04 04:25:15.921估计 [9025:c07] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:无效的节数。更新后表视图中包含的节数(2)必须等于更新前表视图中包含的节数(2),加上或减去插入或删除的节数(0插入,1已删除)。
  • 是的,这正是我在答案中描述的

标签: objective-c uitableview


【解决方案1】:

我猜numberOfSectionsInTableView: 返回之前的节数。您必须确保您告诉表视图执行的操作也发生在数据源中。也就是说,如果您删除了一个部分,那么numberOfSectionsInTableView 也必须返回一个比以前小一的数字。这同样代表细胞和其他一切。这可能在您收到的错误中有所描述。

【讨论】:

    【解决方案2】:

    您必须在numberOfSectionsInTableview:委托方法中管理您的部分数量

    【讨论】:

      【解决方案3】:
      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
      //#warning Potentially incomplete method implementation.
          // Return the number of sections.
          return 1;
      }
      
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      //#warning Incomplete method implementation.
          // Return the number of rows in the section.
          return  [MLControl shared].arrBuyList.count;//1;//[MLControl shared].arrBuyList.count;
      }
      
      // Override to support editing the table view.
      - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
          if (editingStyle == UITableViewCellEditingStyleDelete) {
              // Delete the row from the data source
             // [tableView beginUpdates];
              NSLog(@"indexPath.row=%ld,indexPath.se=%ld",(long)indexPath.row,(long)indexPath.section);
              [[MLControl shared].arrBuyList removeObjectAtIndex:indexPath.row];
              [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
          //    [tableView endUpdates];
          } 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
          }   
      }
      

      【讨论】:

        【解决方案4】:

        我遇到了这个问题,但原因和你的不一样。

        当我为 tableView 添加一个部分时,我用这个方法重新加载数据,这也会像你的那样崩溃日志记录:

        [tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];
        

        为什么我要使用这种方法来重新加载我的表?如果我只刷新单元格,这可以正常工作,但我忘记了我想更改这些部分。欲了解更多信息:What is More reliable way to reload UITableView Data?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-30
          • 2012-03-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多