(很高兴接受Swift或Objective-C的答案)
我的表视图有几个部分,当按下一个按钮时,我想在第0部分的末尾插入一行.再次按下该按钮,我想删除同一行.我几乎工作的代码看起来像这样:
// model is an array of mutable arrays, one for each section
- (void)pressedAddRemove:(id)sender {
self.adding = !self.adding; // this is a BOOL property
self.navigationItem.rightBarButtonItem.title = (self.adding)? @"Remove" : @"Add";
// if adding, add an object to the end of section 0
// tell the table view to insert at that index path
[self.tableView beginUpdates];
NSMutableArray *sectionArray = self.model[0];
if (self.adding) {
NSIndexPath *insertionPath = [NSIndexPath indexPathForRow:sectionArray.count inSection:0];
[sectionArray addObject:@{}];
[self.tableView insertRowsAtIndexPaths:@[insertionPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// if removing, remove the object from the end of section 0
// tell the table view to remove at that index path
} else {
NSIndexPath *removalPath = [NSIndexPath indexPathForRow:sectionArray.count-1 inSection:0];
[sectionArray removeObject:[sectionArray lastObject]];
[self.tableView deleteRowsAtIndexPaths:@[removalPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
[self.tableView endUpdates];
}
Run Code Online (Sandbox Code Playgroud)
这有时会正常运行,但有时不会,这取决于滚动表视图的位置:
我可以通过"Debug-> Toggle Slow Animations"在慢动作模拟器中看到这种情况.删除时反向出现同样的问题.
我发现偏移量的跳跃大小与表格滚动到第0部分的距离有关:当偏移很小时跳跃很小.当滚动接近第0部分总高度的一半时,跳跃变大(问题在于它在这里最差,跳跃= =部分高度的一半).进一步滚动,跳跃变小.当滚动表时,只有少量的0部分仍然可见,跳跃很小.
你能帮我理解为什么会这样,以及如何解决?
Gaé*_*anZ 36
在iOS 11上,UITableView使用估计的行高作为默认值.
插入/重新加载或删除行时会导致不可预测的行为,因为UITableView在大多数情况下的内容大小错误:
为了避免过多的布局计算,tableView heightForRow仅询问每个cellForRow调用并记住它(在正常模式下,tableView要求tableView heightForRow的所有indexPath).其余单元格的高度等于该estimatedRowHeight值,直到cellForRow调用它们为止.
// estimatedRowHeight mode
contentSize.height = numberOfRowsNotYetOnScreen * estimatedRowHeight + numberOfRowsDisplayedAtLeastOnce * heightOfRow
// normal mode
contentSize.height = heightOfRow * numberOfCells
Run Code Online (Sandbox Code Playgroud)
一种解决方案是estimatedRowHeight通过将estimatedRowHeight设置为0并heightForRow为每个单元格实现来禁用该模式.
当然,如果您的单元格具有动态高度(大部分时间使用繁重的布局计算,因此您使用estimatedRowHeight的原因很充分),您必须找到一种方法来重现estimatedRowHeight优化,而不会影响tableView的contentSize.看一下AsyncDisplayKit或UITableView-FDTemplateLayoutCell.
另一个解决方案是尝试找到一个estimatedRowHeight适合的.特别是在iOS 11上,您可以尝试使用UITableView.automaticDimensionestimatedRowHeight:
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = UITableView.automaticDimension
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9205 次 |
| 最近记录: |