我想在完成表演后滚动到UITableView的底部 [self.tableView reloadData]
我原本有
[self.tableView reloadData]
NSIndexPath* indexPath = [NSIndexPath indexPathForRow: ([self.tableView numberOfRowsInSection:([self.tableView numberOfSections]-1)]-1) inSection: ([self.tableView numberOfSections]-1)];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
Run Code Online (Sandbox Code Playgroud)
但后来我读了reloadData是异步的,所以滚动不因为发生self.tableView
,[self.tableView numberOfSections]
并且[self.tableView numberOfRowsinSection
都是0.
谢谢!
有什么奇怪的是我正在使用:
[self.tableView reloadData];
NSLog(@"Number of Sections %d", [self.tableView numberOfSections]);
NSLog(@"Number of Rows %d", [self.tableView numberOfRowsInSection:([self.tableView numberOfSections]-1)]-1);
Run Code Online (Sandbox Code Playgroud)
在控制台中它返回Sections = 1,Row = -1;
当我完成相同的NSLogs时,cellForRowAtIndexPath
我得到Sections = 1和Row = 8; (8是对的)
我觉得这可能是一个普遍的问题,并且想知道是否有任何共同的解决方案.
基本上,我的UITableView具有每个单元格的动态单元格高度.如果我不在UITableView和我的顶部tableView.reloadData()
,向上滚动变得有点跳跃.
我相信这是因为我重新加载数据,因为我正在向上滚动,UITableView会重新计算每个进入可见性的单元格的高度.如何缓解这种情况,或者如何仅将数据从某个IndexPath重新加载到UITableView的末尾?
此外,当我设法一直滚动到顶部时,我可以向下滚动然后向上滚动,没有跳跃没问题.这很可能是因为已经计算了UITableViewCell高度.
(很高兴接受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 …
Run Code Online (Sandbox Code Playgroud) 我有一个表格视图,每个单元格有可能有自己的高度,因此不适合使用rowHeight
.相反,现在我正在使用let indexSet = NSIndexSet(index: 10)
,而且self.tableView.estimatedRowHeight = 75
.这意味着它调用sizeThatFits
单元格上的函数,以确定其高度.这一切都运作良好.
问题是当你重新加载屏幕上的单元格时.例如,向下滚动以显示单元格10,然后重新加载单元格10,工作正常.但是当你开始向上滚动,经过你已经看过的单元格时,它会恢复到每个单元格的estimatedRowHeight,完全忽略sizeThatFits
,因此在滚动时跳转.我不可能给出一个准确或"足够好"的估计值,因此这种跳跃不会引人注意,因为我的细胞能够显示一行文字或一个完整的图像 - 这是一个很大的区别.尺寸.
我在这里展示了这个效果:
我在这方面做了很多不同的尝试,使用了heightForRowAtIndexPath,estimatedHeightForRowAtIndexPath等的混合物.我在StackOverflow上尝试了各种建议.似乎没什么用.
我附上了一个非常简单的示例项目,您可以自己尝试:
https://www.dropbox.com/s/8f1rvkx9k23q6c1/tableviewtest.zip?dl=0
值得注意的是 - 如果单元格在重新加载时不在视图中,则不会发生这种情况.如果它高于或低于当前滚动点,则一切都按预期工作.
当我升级到iOS 11时,我的应用程序的tableview突然无法顺利滚动.每当我滚动(或reloadData
)时,tableview将突然跳转到随机内容偏移位置.
我使用mopub的布局器将广告插入到tableview中,我注意到每当应用程序执行时[tableview reloadData]
,tableview滚动偏移都会出错并跳转到随机位置.
uitableview ×5
ios ×4
objective-c ×2
swift ×2
autolayout ×1
ios11 ×1
iphone ×1
reloaddata ×1
uiscrollview ×1