我在点击表格 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/