向上无限滚动,用于底部首先填充的UITableView

时间:2016-02-23 13:09:19

标签: ios objective-c swift uitableview

我正在尝试构建类似于Facebook Messenger的即时消息,底部有最新消息,用户向上滚动时会加载旧消息。

感谢Stackoverflow上的许多其他帖子,我可以获得无限滚动和底部首先填充的表。不幸的是,我似乎无法让他们两个同时工作。

对于无限滚动,我使用这些代码:

override func scrollViewDidScroll(scrollView: UIScrollView) {
    let contentOffset = scrollView.contentOffset.y
    let maxOffset = scrollView.contentSize.height - scrollView.frame.size.height
    if (maxOffset - contentOffset) <= (scrollView.contentSize.height) * 0.2 {
        // Code for loading more data
    }
}

我也使用过这段代码:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if (self.newsfeeds.count - indexPath.row) < 4 {
        // Code for loading more data
    }
}

对于底部首先填充的表格,我在tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell中使用了这个简单的代码:

let message = messages[messages.count - 1 - indexPath.row]

viewWillAppear

中的这一行配对
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.messages.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: false)

用文字描述,如果用户到达某个单元格或y位置,该应用程序将加载更多数据。但是我的底部首先填充的表通过从顶部到底部向单元格添加反向排序的数据,然后立即向下滚动到底部来显示该区域。

因此应用程序点击表格的顶部,然后立即点击表格底部,反复触发加载更多数据的代码,如某些疯子。

有没有其他方法可以实现底部首先填充的表格或某种方法来解开这个混乱?我很感激任何帮助!

0 个答案:

没有答案