首页 > 解决方案 > 如何知道该行何时位于视图顶部以在 UITableView Swift 中获取更多数据?

问题描述

我有一个用 Swift 编写的具有 UITableView 的 IOS 消息传递应用程序。消息按日期时间从最旧的顶部和最近的底部显示。

当行索引为 0 时,我正在加载更多数据,但我无法弄清楚它是如何工作的。

当用户第一次加载 messageViewController 时,它会下载该对话的消息,如下所示:

internal func messagesDownloaded(page: MessagePage) {
   //append page.content to messages (content) array
  for index in 0..<page.content.count {
     self.content.insert(page.content[index], at: index)
  }
  self.progressBar.isHidden = true
  self.LOG.info("reload table")
  self.tableView.reloadData()     

  scrollToBottom()     
}

然后,如果 ui 位于顶部或 row 为 0(零),则使用以下函数 fetchMore() 数据:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if (tableView.numberOfRows(inSection: 0) - 1) == indexPath.row {
         LOG.info("the top has reached: row: \(indexPath.row)")
         downloadCheck(indexPath.row)
    }
} 

但是,当第一行可见时,if 子句永远不会进入,所以我知道何时获取更多数据。什么是最好的方法?

标签: swiftuitableview

解决方案


推荐阅读