8

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

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

    NSIndexPath* ipath = [NSIndexPath indexPathForRow: [self.messageOnTimeline numberOfRowsInSection:0]-1 inSection: 0];
    [self.messageOnTimeline scrollToRowAtIndexPath: ipath atScrollPosition: UITableViewScrollPositionTop animated: YES];

在我向 tableview 添加标题之前,这一直很好。我添加了以下代码,现在加载应用程序时它不会滚动到底部:

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

有谁知道为什么?

非常感谢您的帮助

4

3 回答 3

7

在你添加这个代码viewDidload

斯威夫特 5

let indexPath = IndexPath(item: <Row Index>, section: <Section Index>)
tableView.reloadRows(at: [indexPath], with: .automatic)

目标 C

NSIndexPath *myIndexPath = [NSIndexPath indexPathForRow:AddYourLastIndex inSection:0];
[self.tbl_presentation selectRowAtIndexPath:myIndexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
于 2013-09-09T10:41:20.523 回答
3

试试这个代码。它将滚动到表格视图的底部

 [self.tableview setContentOffset:CGPointMake(0, self.tableview.contentSize.height-self.tableview.frame.size.height) animated:YES];
于 2013-09-09T16:09:58.590 回答
0

@Darshan Kunjadiya 答案是正确的。即使我们可以在故事板中实现相同的效果。

选择 tableView --> 从情节提要控件中单击“添加缺少的约束”。这会将约束添加到 tableView 和 View。

这帮助我解决了这个问题。附上屏幕截图以供参考。 screen_shot 链接

于 2014-07-28T09:22:28.957 回答