确定滚动时 UITableViewController 的哪些部分是可见的

Asy*_*nc- 0 xcode uitableview ios swift2

我试图在 swift 2 中确定 UITableView 的哪个部分是可见的(在顶部,如果同时有 2 个部分)。到目前为止,我找到了这个解决方案: 有没有办法知道用户是向上还是向下滚动 tableview ? 在 swift IOS 中,这个:如何获取 UITableView 的可见部分?还有这个: Swift - UITableView 滚动事件

由于以下原因,他们都不为我工作:

1.我不能添加UIScrollViewDelegate,因为swift 2在冗余符合协议时给出错误(因为它是UITalbeView,并且它已经符合UIScrollView)。

2.然而,这:

func scrollViewDidScroll () {
    print("scrolled: \(self.tableView.indexPathsForVisibleRows)")
}
Run Code Online (Sandbox Code Playgroud)

永远不会被推出。

UITableView 的委托和数据源是通过故事板界面构建器定义的。

你如何在swift 2中确定UITableViewController 正在滚动,哪个部分是可见的?

Vvk*_*Vvk 5

请尝试从willDisplayCell方法中获取可见单元格,然后从该可见单元格中获取部分。例如:首先从 Cell 中获取 Indexpath,然后从 Indexpath 中检索部分。

让 section = indexPath.section

代码是:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomCellViewController

    let position: CGPoint = cell.convertPoint(CGPointZero, toView: self.tableView)
    if let indexPath = self.tableView.indexPathForRowAtPoint(position)
    {
        let section = indexPath.section
        print("will display section: \(section)")
    }
}
Run Code Online (Sandbox Code Playgroud)