【问题标题】:Reranging a uitableview重新排列 uitableview
【发布时间】:2010-06-10 13:41:03
【问题描述】:

我遇到了同样的问题:Crashing while trying to move UITableView rows! 我要做的就是在特定的重新排列操作上插入一个新单元格或删除选定的单元格。

有时我会得到:

********* -[_UITableViewUpdateSupport _setupAnimationForReorderingRow] 中的断言失败,/SourceCache/UIKit/UIKit-963.10/UITableViewSupport.m:295 尝试 为单元格异常创建两个动画

当我想通过调用 [self.tableView reloadData] 重新加载我的数据时 我明白了:

********** 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)”

有没有人知道如何处理这个问题?

【问题讨论】:

  • 和...一样的问题?没有代码很难说。也许使用调试器并查看它崩溃的堆栈跟踪。可以是任何事情,从不提供细胞到内存管理不善。

标签: objective-c iphone uitableview cocoa-touch uikit


【解决方案1】:

你好!

感谢您的所有帮助! 我实际上找到了解决问题的方法!

我不再关心在编辑时为 tableview 设置动画了! 我终于在这篇帖子中找到了解决方案:How to stop UITableView moveRowAtIndexPath from leaving blank rows upon reordering

汤姆萨克斯顿说:

// APPLE_BUG:如果您将单元格移动到屏幕外的行(因为目标 // 已被修改),伪造的单元格被创建并最终导致崩溃

所以我的代码如下所示:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
if( fromIndexPath == toIndexPath ) {
    return;
}

if (toIndexPath.row == [self.data count] -1) { //element has been dragged to the bottom
        self.data removeObjectAtIndex:fromIndexPath.row];
        [self performSelector:@selector(delayedReloadData:) withObject:tableView afterDelay:0];
}
else { //Normal reoder operation
        [self.data removeObjectAtIndex:fromIndexPath.row];
     [self.data insertObject:element atIndex:toIndexPath.row];
}

}

- (void)delayedReloadData:(UITableView *)tableView {
//Assert(tableView == self.tableView);
[self.tableView reloadData];

}

我的 initData 方法中也有内存泄漏,我这样解决了:

-(void) initData {
if (!self.data) {
    self.data = [[NSMutableArray alloc] init];
}
else {
    [self.data removeAllObjects];
}

[self.data addObjectsFromArray:[self.fetchedResultsController fetchedObjects]];

}

感谢您的帮助!

尼古拉

【讨论】:

    【解决方案2】:

    initData 中仍有内存泄漏。我假设您将数据定义为“@property (nonatomic, retain) NSMutableArray *data”。因此,当您调用 alloc/init 时,保留计数为 1,然后分配给执行保留的 self.data,使其计数为 2。您需要这样做:

    NSMutableArray *ary = [[NSMutableArray alloc] init];
    self.data = ary;
    [ary release];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-12
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多