[英]Get indexPath of “next” UITableViewCell
点击表格视图的单元格时,我有一些代码。 在某些情况下,我想为下一个单元格递归调用函数tableView(_, didSelectRowAtIndexPath)
。 这意味着当我选择第 5 行时,我想选择第 6 行,依此类推。
如何根据另一行获取下一个单元格的 indexPath?
这是斯威夫特的答案:
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
自定义单元格的UITextField
。 它配置了一个 Next 按钮,当点击该按钮时,焦点将移动到下一个UITextField
。
去之前的indexPath,看这个回答: https ://stackoverflow.com/a/56867271/
对于包含上一个/下一个按钮作为键盘上方工具栏的示例项目,请查看示例项目: https : //github.com/bvankuik/TableViewWithTextFieldNextButton
对于之前的 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)
}
}
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
}
这将适用于上一个和下一个的 swift 4
let nextIndexPath = IndexPath(row: indexPath.row + 1, section: indexPath.section)
let previousIndexPath = IndexPath(row: indexPath.row - 1, section: indexPath.section)
我写了一个 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()
}
...
对于那些喜欢@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)
}
}
目前,在我看来,只有(?)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
}
}
你可以得到 IndexOFObeject
NSUInteger indexOfTheObject = [Array indexOfObject:indexPath];
和单元格抽头:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *temp = [Array objectAtIndex:indexPath.row+1];
temp...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.