【问题标题】:{tableView reloadData} causes app to crash after section is deleted{tableView reloadData} 导致应用在删除部分后崩溃
【发布时间】:2011-06-08 02:50:10
【问题描述】:

我在方法中以编程方式定义了一个自定义标题视图: 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:]: index 1 beyond bounds [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(Internal)_sendActionsForEvents:withEvent:]

重载数据抛出异常。我该如何解决?任何建议,将不胜感激。

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    当您删除对象时,您是否减少了您的表委托报告的表中存在的项目数?

    这一行:

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

    正在对数组执行 objectAtIndex,可能是您刚刚从中删除条目的数组。我猜它要求的数组元素比现有的多一个。

    【讨论】:

    【解决方案2】:

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

    这可能是在尝试删除不存在的索引时发生的

    【讨论】:

      【解决方案3】:

      检查是否

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多