点击表格视图的单元格时,我有一些代码。在某些情况下,我想以递归方式为下一个单元调用函数tableView(_, didSelectRowAtIndexPath)
。这意味着当我选择第5行时,我想选择第6行等等。
如何根据另一行获取 next 单元格的indexPath?
答案 0 :(得分:5)
这是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
}
我使用此代码是因为我有一个包含UITextField
的自定义单元格的tableview。它已配置了“下一步”按钮,当点按该按钮时,焦点将移至下一个UITextField
。
有关完整代码,请查看示例项目: https://github.com/bvankuik/TableViewWithTextFieldNextButton
答案 1 :(得分:1)
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
}
答案 2 :(得分:1)
对于以前的indexPath,我在UITableView上进行了以下扩展 (Swift 5.0)
extension UITableView {
func previousIndexPath(currentIndexPath: IndexPath) -> IndexPath? {
let startRow = currentIndexPath.row
let startSection = currentIndexPath.section
var previousRow = startRow
var previousSection = startSection
if startRow == 0 && startSection == 0 {
return nil
} else if startRow == 0 {
previousSection -= 1
previousRow = numberOfRows(inSection: previousSection) - 1
} else {
previousRow -= 1
}
return IndexPath(row: previousRow, section: previousSection)
}
}
答案 3 :(得分:0)
这将在swift 4中运行 上一个和下一个
let nextIndexPath = IndexPath(row: indexPath.row + 1, section: indexPath.section)
let previousIndexPath = IndexPath(row: indexPath.row - 1, section: indexPath.section)
答案 4 :(得分:0)
我写了一个IndexPath扩展方法,发现它的逻辑比@Bart van Kuik的解决方案更容易理解。
用Swift 5,Xcode 11编写,适用于多节UITableView。
import UIKit
extension IndexPath {
// Helper Methods
func incrementRow(plus: Int=1) -> IndexPath {
return IndexPath(row: row + plus, section: section)
}
func incrementSection(plus: Int=1) -> IndexPath {
return IndexPath(row: 0, section: section + plus)
}
func next(in table: UITableView) -> IndexPath? {
// if can find cell for next row, return next row's IndexPath
if let _ = table.cellForRow(at: incrementRow()) {
return incrementRow()
}
// cannot find next row, try to find row 0 in next section
else if let _ = table.cellForRow(at: incrementSection()) {
return incrementSection()
}
// can find neither next row nor next section, the current indexPath is already the very last IndexPath in the given table
return nil
}
}
对于以前的IndexPath,@ Bishal Ghimire的答案是正确的,但这是IndexPath版本扩展。
func previous(in table: UITableView) -> IndexPath? {
// if the current indexPath is the very first IndexPath, then there's no previous
if row == 0 && section == 0 { return nil }
// if the current indexPath is the first row in a section, return table's previous section's last row's IndexPath
if row == 0 {
let lastRowInPrevSection = table.numberOfRows(inSection: section - 1) - 1
return IndexPath(row: lastRowInPrevSection, section: section - 1)
}
// else just return previous row's IndexPath in the same section
else {
return IndexPath(row: row - 1, section: section)
}
}
您可以将这些方法拖放到您的任何项目中,然后直接使用它们,在我的情况下,我试图在用户按下回车键时突出显示下一个单元格的textField,因此用法如下:
...
if let nextIndexPath = currentIndexPath.next(in: myTableView),
let nextCell = myTableView.cellForRow(at: nextIndexPath) as? MyCell {
nextCell.textField.becomeFirstResponder()
} else {
// there's no next IndexPath in the given table, simply resign first responder for the current cell's textField
currentCell.textField.resignFirstResponder()
}
...
答案 5 :(得分:0)
对于喜欢@Bishal Ghimire 的 previousIndexPath()
方法的人来说,这就是 nextIndexPath()
方法。
import UIKit
extension UITableView {
func nextIndexPath(currentIndexPath: IndexPath) -> IndexPath? {
let startRow = currentIndexPath.row
let startSection = currentIndexPath.section
var nextRow = startRow
var nextSection = startSection
if startSection == numberOfSections-1 && startRow == numberOfRows(inSection: startSection)-1 {
return nil
} else if startRow == numberOfRows(inSection: startSection)-1 {
nextSection += 1
nextRow = 0
} else {
nextRow += 1
}
return IndexPath(row: nextRow, section: nextSection)
}
}
答案 6 :(得分:0)
目前,在我看来,只有(?)Bart van Kuiks 的回答目前考虑了一种可能性,即一个部分可能不包含任何行。
其他发帖人可能会更正他们的答案。同时,我将下一个和上一个单元格的代码发布为 UITableView
-Extensions。如果您发现任何错误,请随时编辑代码。
extension UITableView {
func indexPathOfCell(after indexPath: IndexPath) -> IndexPath? {
var row = indexPath.row + 1
for section in indexPath.section..<numberOfSections {
if row < numberOfRows(inSection: section) {
return IndexPath(row: row, section: section)
}
row = 0
}
return nil
}
func indexPathOfCell(before indexPath: IndexPath) -> IndexPath? {
var row = indexPath.row - 1
for section in (0...indexPath.section).reversed() {
if row >= 0 {
return IndexPath(row: row, section: section)
}
if section > 0 {
row = numberOfRows(inSection: section - 1) - 1
}
}
return nil
}
}
答案 7 :(得分:-1)
您可以获得IndexOFObeject
NSUInteger indexOfTheObject = [Array indexOfObject:indexPath];
和Cell tap:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *temp = [Array objectAtIndex:indexPath.row+1];
temp...
}