ios - 当所述行靠近表格 View 的底部时,ScrollToRow 不起作用

标签 ios swift uitableview scroll indexpath

我有一个实现文本字段委托(delegate)的单元类。在这个委托(delegate)中,我正在调用一个函数来告诉 tableview 根据 indexPath 滚动到特定行。这在大多数情况下有效,但在行位于表格 View 底部时无效。单元类有一个在我的主 Controller cellForRow 方法中传入的表属性。下面的代码:

extension IR_TextCell: UITextFieldDelegate {

    func textFieldDidBeginEditing(_ textField: UITextField) {
        util_textField.addHightlightedBorder(textField)
        if let index = table?.indexPath(for: self)
        {
            scrollDelegate?.scrollToMe(at: index)
        }
    }
}

func scrollToMe(at index: IndexPath) {
    self.tableV.scrollToRow(at: index, at: .middle, animated: false)
}

我已经尝试围绕这个包装 DispatchQueue.main.async 并添加截止日期,但它没有任何区别。

我是否需要更改我的 tableview 的底部约束?

最佳答案

我的情况与您的情况有些不同,但我在滚动到靠近底部的单元格时遇到了同样的问题。它现在可能适用于您的确切情况,但我希望这对遇到此帖子的人有所帮助。我怀疑这可能是时间问题,所以我最终这样做如下:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.row == tableView.indexPathsForVisibleRows?.last?.row {

        let scrollIndex = 0//set to your predetermined scrolled to index
        let cellRect = tableView.rectForRow(at: indexPath)
        let completelyVisible = tableView.bounds.contains(cellRect)

        if scrollIndex >= indexPath.row && !completelyVisible {
            let maxIndex = 10//number of elements in the array - 1
            //in case you want a delay
            DispatchQueue.main.asyncAfter(deadline: .now() + 0)  {
                self.tableView.scrollToRow(at: IndexPath(row: scrollIndex > maxIndex ? maxIndex : scrollIndex, section: 0), at: .middle, animated: false)
            }
        }

    }
}

关于ios - 当所述行靠近表格 View 的底部时,ScrollToRow 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47410473/

相关文章:

ios - swift ios 我什么时候需要使用 mediaTypes kUTTypeImage

ios - Swift 中的自定义导航栏

objective-c - 是否可以刷新 UITableView 中的单个 UITableViewCell?

ios - 使用 reloadRowsAtIndexPaths 或 reloadSections 导致 Tableview Cell 闪烁

ios - Facebook 上的视频分享

ios - NSURLConnection sendSynchronousRequest 似乎挂起

android - iOS 或 Android 应用程序可以与其他应用程序交互推送通知吗?

iphone - 如何以安全的方式为其他开发人员构建框架或库?

ios - 如何在iOS上的mailto正文中插入换行符?

ios - 仅用于单个表部分的动态 UITableViewCell 高度