我有一个带有单元格的UITableView,当用户点击它们时,它们会展开/折叠,使用:
-(void)tableView:(UITableView *)_tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
...
}在运行reloadRowsAtIndexPath之后,我让heightForRowAtIndexPath根据单元格是折叠还是展开返回不同的高度:
-(CGFloat)tableView:(UITableView *)_tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// if expand
return 200;
// if collapse
return 49;
}当我折叠表格底部的一个单元格时,表格会缩小,但它会缩小得太远,在表格的顶部留下大量的空白。在调用reloadRowsAtIndexPaths之后,我尝试了[tableView scrollToRowAtIndexPath:indexPath],它解决了这个问题,但这会使表变得不稳定。它会往下走太远,留下空格,然后跳回顶部。如何在不使表格单元格向下滚动太远的情况下缩小表格单元格?
发布于 2018-09-25 07:21:16
更改影响tableView高度的内容后,可以使用这些代码来更新单元格高度,而无需重新加载表格:
[tableView beginUpdates];
[tableView endUpdates];发布于 2018-09-25 13:44:56
也许您可以在主线程中使用UIView动画来避免这种情况,例如:
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.3 animations:^{
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}];
});然后尝试:
if (@available(iOS 11.0, *)) {
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
}https://stackoverflow.com/questions/52488376
复制相似问题