【问题标题】:Obtain the indexPath of UITableView's cell when scrolling滚动时获取UITableView的cell的indexPath
【发布时间】:2016-06-21 20:06:49
【问题描述】:

当您在表格视图中滚动时,它不会在您手指所在的单元格上开始表格委托方法 didSelectRowAtIndexPath 方法。如果我要点击该调用,而不是向上/向下滑动它,它会授予我具有委托方法的 indexPath。

我想知道在滚动 tableView 时是否可以获得手指所在单元格的 indexPath。这可能吗?

【问题讨论】:

    标签: ios tableview


    【解决方案1】:

    这可能是要走的路:

    class TableViewController: UITableViewController {
    
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 50
        }
    
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            var cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("Cell")
            if cell == nil {
                cell = UITableViewCell(style: .Default, reuseIdentifier: "Cell")
            }
    
            cell.textLabel?.text = "\(indexPath.row)"
    
            return cell
        }
    
        override func scrollViewWillBeginDragging(scrollView: UIScrollView) {
            let location = scrollView.panGestureRecognizer.locationInView(tableView)
            guard let indexPath = tableView.indexPathForRowAtPoint(location) else {
                print("could not specify an indexpath")
                return
            }
    
            print("will begin dragging at row \(indexPath.row)")
        }
    
    }
    

    【讨论】:

    • 很高兴我能帮上忙。一些学分归于@Wain,因为他指出了正确的方向......;)
    【解决方案2】:

    我猜你可以访问表格视图的panGestureRecognizer(因为它是一种滚动视图)并从中获取locationInView:。然后,您可以使用indexPathForRowAtPoint: 要求表格视图将其转换为索引路径。

    【讨论】:

    • 我会试试的。谢谢
    猜你喜欢
    • 1970-01-01
    • 2012-03-23
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多