【问题标题】:TableView deleteRowsAtIndexPaths:withRowAnimation Very SlowTableView deleteRowsAtIndexPaths:withRowAnimation 非常慢
【发布时间】:2014-09-05 09:35:38
【问题描述】:

我有一张桌子,它可能有几百行,所有的高度都不同。每当用户删除一行时,调用:

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

执行需要很长时间(大约 2.5 秒)。

是否有替代调用这个或任何方法来加快它?谢谢!

【问题讨论】:

  • 这个方法还会调用数据源委托的一些方法,我想这就是卡住的地方,你能展示一下你是如何实现UITableView的数据源委托的吗?
  • 一定要在主线程调用

标签: ios uitableview delete-row


【解决方案1】:

使用UITableView的方法estimatedRowHeight来加速。

提供行高的非负估计可以提高加载表视图的性能。如果表格包含可变高度的行,则在表格加载时计算它们的所有高度可能会很昂贵。使用估算可以让您将几何计算的一些成本从加载时间推迟到滚动时间。

在任何情况下,您都应该使用 Instruments 来衡量您的代码中哪些部分占用的时间最多。

【讨论】:

  • 到目前为止,这似乎是最好的选择。它确实减少了时间,尽管几乎没有不良影响。它现在打破了约束,并且在表格顶部比底部更快(我猜它必须加载它上面的所有高度)。谢谢!
  • 编辑:通过添加更准确的高度来修复约束问题。还修复了我的桌子加载缓慢。问题仍然存在于表格底部,但速度要快得多(0.5 秒而不是 2.5 秒)
【解决方案2】:

你可以试试这个。这有点不道德,但它就像一种魅力。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 if([textArray count]>0){
  [self deleteRowAtIndexPath:indexPath.row];
} //method fired when u select a row, which calls a method
}

-(void)deleteRowAtIndexPath:(int) indexPath{ //this method removes the data in the array from which the tableview is being populated
[textArray removeObjectAtIndex:indexPath];
[self.tableView reloadData]; //after deleting the data from the array it reloads the tableview
 }

textArray 是一个 NSMutableArray,从中填充 tableview。

但是这个 dsnt 有任何动画。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 2017-12-12
    • 2015-06-14
    • 2013-05-14
    • 2021-05-24
    相关资源
    最近更新 更多