我有一个代码,用于点击表视图的单元格。在某些情况下,我想要递归调用tableView(_, didSelectRowAtIndexPath)
函数以选中下一个单元格。这意味着当我选择第5行时,我想选择第6行,以此类推。
如何基于另一行获取下一个单元格的indexPath?
我有一个代码,用于点击表视图的单元格。在某些情况下,我想要递归调用tableView(_, didSelectRowAtIndexPath)
函数以选中下一个单元格。这意味着当我选择第5行时,我想选择第6行,以此类推。
如何基于另一行获取下一个单元格的indexPath?
这里是一个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
的自定义单元格表视图。它配置了一个“下一个”按钮,当点击该按钮时,焦点会移动到下一个UITextField
。对于之前的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)
}
}
numberOfRows(...) - 1
)吗? - andreas1724currentIndexPath
不是问题所在。我同意你的观点,即currentIndexPath
没有指向零行部分。但请考虑以下情况:第0节有一行。第1节有零行(这是有效的)。第2节有一行。currentIndexPath
指向第2节的第一行。如果您尝试计算前面的单元格IndexPath,则会得到IndexPath [1,-1]。它应该是[0,0]。 - andreas1724 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
}
let nextIndexPath = IndexPath(row: indexPath.row + 1, section: indexPath.section)
let previousIndexPath = IndexPath(row: indexPath.row - 1, section: indexPath.section)
目前,我认为只有Bart van Kuiks的答案考虑了一个部分可能不包含任何行的情况。
其他帖子的作者可能会更正他们的回答。与此同时,我发布了我的代码,用于下一个和上一个单元格作为 UITableView
扩展。如果您发现任何错误,请随意编辑代码。
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
}
}
我写了一个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
}
}
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)
}
}
...
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)
}
}
您可以获取Object的IndexOFO
NSUInteger indexOfTheObject = [Array indexOfObject:indexPath];
对于单元格点击:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *temp = [Array objectAtIndex:indexPath.row+1];
temp...
}