【问题标题】:iOS - UITableView not scrolling to bottomiOS - UITableView 不滚动到底部
【发布时间】:2013-09-12 19:53:45
【问题描述】:

我正在 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;
}

有人知道为什么吗?

非常感谢您的帮助

【问题讨论】:

    标签: ios uitableview header scroll scrollto


    【解决方案1】:

    将此代码添加到您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];
    

    【讨论】:

      【解决方案2】:

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

       [self.tableview setContentOffset:CGPointMake(0, self.tableview.contentSize.height-self.tableview.frame.size.height) animated:YES];
      

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-11-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-22
          • 2012-01-07
          相关资源
          最近更新 更多