获取"next"UITableViewCell的indexPath

bor*_*ero 3 uitableview ios swift

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

如何根据另一行获取下一个单元格的indexPath ?

Bar*_*uik 6

这是Swift 4中的答案:

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
}
Run Code Online (Sandbox Code Playgroud)

我使用这段代码,因为我有一个包含自定义单元格的tableview UITextField.它配置了下一个按钮,当点击该按钮时,焦点移动到下一个按钮UITextField.

有关完整代码,请查看示例项目:https: //github.com/bvankuik/TableViewWithTextFieldNextButton


Oni*_* IV 2

     override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let nextIndexPath=NSIndexPath(forRow: indexPath.row + 1, inSection: indexPath.section);

    // You should be sure than this NSIndexPath exist, and ...make what you want

}
Run Code Online (Sandbox Code Playgroud)

  • 如果下一行位于下一部分中,则此操作将不起作用。 (2认同)