{tableView reloadData}导致应用程序在删除部分后崩溃

时间:2011-06-08 02:50:10

标签: ios uitableview

我在方法中以编程方式定义了一个自定义标题视图: ViewForHeaderInSection 在自定义视图中是一个UIButton,它有一个目标动作method:"foodDeleteButtonPressed"。我也将按钮的标签变量设置为节号。

以下是该方法的实现

-(void)foodDeleteButtonPressed:(id)sender
{
NSInteger index = [sender tag];
NSLog(@"Delete button pressed for section: %d", index);

FoodItem *tmpItem = [Cart.foodItemsArray objectAtIndex:index];

int ct = [tmpItem.sidesArray count];
if(ct > 0)
   {
    NSLog(@"About to delete %d sides", ct);
    [tmpItem.sidesArray removeAllObjects];
   }
else 
   {
    NSLog(@"The fooditem to be deleted has no side items");
   }

NSLog(@"About to remove object from ffod items array");

[Cart.foodItemsArray removeObjectAtIndex:index];
[self.myOrderTable reloadData];


}

此方法在没有行的情况下执行正常: [self.myOrderTable reloadData];

我尝试重新加载数据后删除部分后尝试刷新视图的那一刻。 我的应用程序崩溃了以下堆栈跟踪:

  

*由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* - [NSMutableArray objectAtIndex:]:索引1超出界限[0 .. 0]'
  ***第一次投掷筹码:
  (
  CoreFoundation 0x010325a9 __exceptionPreprocess + 185
  libobjc.A.dylib 0x01186313 objc_exception_throw + 44
  CoreFoundation 0x010280a5 - [__ NSArrayM objectAtIndex:] + 261
  餐厅0x00015c77 -   [MyOrderViewController tableView:viewForHeaderInSection:] + 827
  UIKit 0x0038de48 - [UITableView(UITableViewInternal)_delegateWantsHeaderForSection:] + 152
  UIKit 0x004d8c01 - [UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 116
  UIKit 0x004d6d88 - [UITableViewRowDatanumberOfRows] + 108
  UIKit 0x0038a677 - [UITableViewnoteNumberOfRowsChanged] + 132
  UIKit 0x00397708 - [UITableView reloadData] + 773
  餐厅0x00011eac - [MyOrderViewControllerfoodDeleteButtonPressed:] + 444
  UIKit 0x0031c4fd - [UIApplicationsendAction:to:from:forEvent:] + 119
  UIKit 0x003ac799 - [UIControlsendAction:to:forEvent:] + 67
  UIKit 0x003aec2b - [UIControl(内部)_sendActionsForEvents:withEvent:]

重装数据引发异常。我该如何解决?任何建议,将不胜感激。

3 个答案:

答案 0 :(得分:0)

删除对象时,是否减少了表委托报告的项目数量?

这一行:

餐厅0x00015c77 - [MyOrderViewController tableView:viewForHeaderInSection:] + 827

正在对一个数组执行objectAtIndex,可能就是你刚从中删除了一个数组。我猜它要求的数组元素多于存在。

答案 1 :(得分:0)

!!! [Cart.foodItemsArray removeObjectAtIndex:index]; !!!

当尝试删除不存在的索引

时,可能会发生这种情况

答案 2 :(得分:0)

检查是否

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat

返回正确的值(可能有NAN)

相关问题