iOS - UITableView无法滚动到底部

Spe*_*her 8 scroll header scrollto uitableview ios

我正在iOS中构建一个聊天应用程序但是我在UITableView上显示所有消息时遇到了一些问题.我希望表视图在加载时滚动到底部,以便用户在打开应用程序时可以看到最新消息.

为此,我在刷新数据的函数中添加了此代码:

    NSIndexPath* ipath = [NSIndexPath indexPathForRow: [self.messageOnTimeline numberOfRowsInSection:0]-1 inSection: 0];
    [self.messageOnTimeline scrollToRowAtIndexPath: ipath atScrollPosition: UITableViewScrollPositionTop animated: YES];
Run Code Online (Sandbox Code Playgroud)

这工作正常,直到我在tableview中添加了一个标题.我添加了以下代码,现在加载应用程序时它不会滚动到底部:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
    return view;
}
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么?

非常感谢您的帮助

Dar*_*iya 7

在您添加此代码viewDidload:

目标C.

 NSIndexPath *myIndexPath = [NSIndexPath indexPathForRow:AddYourLastIndex inSection:0];
[self.tbl_presentation selectRowAtIndexPath:myIndexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
Run Code Online (Sandbox Code Playgroud)

迅速

var myIndexPath: NSIndexPath = NSIndexPath.indexPathForRow(AddYourLastIndex, inSection: 0)
self.tbl_presentation.selectRowAtIndexPath(myIndexPath, animated: true, scrollPosition: UITableViewScrollPositionBottom)
Run Code Online (Sandbox Code Playgroud)

或者检查此链接

我希望这段代码对你有用.