ios - 获取 "next"UITableViewCell 的 indexPath

标签 ios uitableview swift

我在点击表格 View 的单元格时有一些代码。在某些情况下,我想为下一个 单元格递归调用函数tableView(_, didSelectRowAtIndexPath)。这意味着当我选择第 5 行时,我想选择第 6 行,等等。

如何根据另一行获取下一个 单元格的索引路径?

最佳答案

这是 Swift 中的一个答案:

private func nextIndexPath(for currentIndexPath: IndexPath, in tableView: UITableView) -> IndexPath? {
    var nextRow = 0
    var nextSection = 0
    var iteration = 0
    var startRow = currentIndexPath.row
    for section in currentIndexPath.section ..< tableView.numberOfSections {
        nextSection = section
        for row in startRow ..< tableView.numberOfRows(inSection: section) {
            nextRow = row
            iteration += 1
            if iteration == 2 {
                let nextIndexPath = IndexPath(row: nextRow, section: nextSection)
                return nextIndexPath
            }
        }
        startRow = 0
    }

    return nil
}

我使用此代码是因为我有一个带有包含 UITextField 的自定义单元格的表格 View 。它配置有一个 Next 按钮,当点击该按钮时,焦点将移动到下一个 UITextField

要转到上一个 indexPath,请参阅此答案:https://stackoverflow.com/a/56867271/

对于包含上一个/下一个按钮作为键盘上方工具栏的示例项目,请查看示例项目: https://github.com/bvankuik/TableViewWithTextFieldNextButton

关于ios - 获取 "next"UITableViewCell 的 indexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27472249/

相关文章:

ios - WKWEBVIEW 仅显示网站的移动版本

iphone - Objective-C:表格单元消失时是否会发生任何事件?

swift - 删除 UItableView 中多余的空格

ios - 2个数组中的元素相乘和相加

ios - NSNumberFormatter - 如何截断高值的小数位?

ios - NSOuputStream发送旧值- objective-c

ios - 将标题添加到 UIPickerView

xcode - 设置具有正确详细信息的自定义单元格样式,但它不显示详细信息文本标签?

ios - 如何在没有滚动效果的情况下显示最后一个 tableView 单元格

swift - animateTransition 不再适用于 iOS 13 中以模态呈现的 viewController