![](/img/trans.png)
[英]Scrolling to bottom of UITableView (UITableView is in a UITableViewCell)
[英]UITableView not scrolling to bottom
嗨,我打算给你链接一个展示我的问题的视频,因为它有点难以传达。 UITableView没有滚动到底部在视频中我点击了一个按钮,它将项目添加到列表中。 请注意,我没有在第16项停止,并且我仍然在列表中添加了更多未显示的项目。 最后,添加新项目后,列表将自动向上滚动
[_items addObject:[NSString stringWithFormat:@"Trawl %d", count]];
[self.tbl reloadData];
[self.tbl scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:self.items.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
主要问题是在视频结束时,我滚动到最后几个项目,但我无法选择它们。 显然这个问题必须处理UITableView的高度。 我试过在故事板编辑器中更改它,但它没有做任何事情。 有任何想法吗? 提前致谢!
好的,我可以看到和评论,我认为你的UITableView
的高度可能大于设备屏幕大小,这意味着它不会向下滚动,因为项目没有到达UITableView
的底部'的UIScrollView
。
转到IB并设置约束,以便UITableView
等于视图控制器的宽度和高度。
调用它时,很可能reloadData
没有完全执行,因为它假设您可能会进行更多更改并告诉它再次重新加载。
尝试引入零秒延迟,让它有机会运行:
[_items addObject:[NSString stringWithFormat:@"Trawl %d", count]];
[self.tbl reloadData];
double delayInSeconds = 0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self.tbl scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:self.items.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
});
此外,在这种情况下,您不应该使用reloadData
。 这是应该添加行的方式:
[_items addObject:[NSString stringWithFormat:@"Trawl %d", count]];
[self.postsTableView beginUpdates];
[self.postsTableView insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:_items.count - 1] withAnimation:NSTableViewAnimationEffectNone]; // maybe you want some other animation here
[self.postsTableView endUpdates];
double delayInSeconds = 0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self.tbl scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:self.items.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
});
似乎你们大多数人都错了。
它应该是这样的:
[self.tbl scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:self.items.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
也就是说,应该是UITableViewScrollPositionTop,而不是UITableViewScrollPositionBottom。 这里atScrollPosition:UITableViewScrollPositionTop是变更的起源,而不是目的地。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.