Ios 获取“的索引”;“下一步”;UITableViewCell
当点击表格视图的单元格时,我有一些代码。在某些情况下,我想为下一个单元格递归调用函数Ios 获取“的索引”;“下一步”;UITableViewCell,ios,uitableview,swift,Ios,Uitableview,Swift,当点击表格视图的单元格时,我有一些代码。在某些情况下,我想为下一个单元格递归调用函数tableView(\uDidSelectRowatineXpath)。这意味着当我选择第5行时,我想选择第6行,以此类推 如何根据另一行获取下一个单元格的indexPath 您可以获得indexofobe对象 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView(\uDidSelectRowatineXpath)
。这意味着当我选择第5行时,我想选择第6行,以此类推
如何根据另一行获取下一个单元格的indexPath 您可以获得indexofobe对象
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
}
NSUInteger indexOfTheObject = [Array indexOfObject:indexPath];
对于单元抽头:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *temp = [Array objectAtIndex:indexPath.row+1];
temp...
}
以下是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
}
private func nextIndexPath(对于currentIndexPath:IndexPath,在tableView:UITableView中)->IndexPath?{
var nextRow=0
var nextSection=0
var迭代=0
var startRow=currentIndexPath.row
对于currentIndexPath.section..
我之所以使用此代码,是因为我有一个带有自定义单元格的tableview,其中包含UITextField
。它配置了“下一步”按钮,当点击该按钮时,焦点将移动到下一个UITextField
要转到上一步,请参见以下答案:
对于包含上一个/下一个按钮作为键盘上方工具栏的示例项目,请查看示例项目:
这将在swift 4中起作用
上一次和下一次
let nextIndexPath = IndexPath(row: indexPath.row + 1, section: indexPath.section)
let previousIndexPath = IndexPath(row: indexPath.row - 1, section: indexPath.section)
对于前面的indexPath,我在UITableView上做了以下扩展 (Swift 5.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)
}
}
您可以将这些方法拖放到您的任何项目中,并直接使用它们。在我的例子中,当用户点击返回键时,我试图突出显示下一个单元格的文本字段,因此用法如下:
...
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的
previousindexath()
方法的人,下面是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)
}
}
目前,在我看来,目前只有(?)巴特·范·库伊克的答案考虑了这样一种可能性,即一个部分可能不包含任何行。 其他的海报可能会更正他们的答案。同时,我将下一个和上一个单元格的代码发布为
UITableView
-扩展名。如果发现任何错误,请随意编辑代码
扩展UITableView{
func indexPathOfCell(在indexPath:indexPath之后)->indexPath{
var row=indexPath.row+1
对于indexPath.section..indexPath中的节{
var row=indexPath.row-1
对于(0…indexath.section).reversed()中的节{
如果行>=0{
返回IndexPath(行:行,节:节)
}
如果节>0{
行数=行数(第1节)-1
}
}
归零
}
}
当您获得indexPath时,根据indexPath+1执行所有操作。UITableView中有多个节还是单节?单节-@Siriss我不能只向indexPath添加1-它不是Int值或类似的值。虽然这个答案可以解决问题,但您应该详细说明一下,并解释代码的作用。@DavidRönnqvist,我很抱歉,但我明白这是一个问题:“如何基于另一行获取下一个单元格的indexPath?”。这是答案:“让nextIndexPath=NSIndexPath(forRow:indexPath.row+1,inSection:indexPath.section);”。这家伙没有任何代码,除了没有工作或代码中有错误。无论如何,我不知道如何提供更多解释。抱歉。这正是我要找的!不知道您可以初始化NSIndexPath之类的this@OnikIV正如我所说的:这段代码可能会解决老年退休金计划的问题,但如果你能详细说明和解释,这将是一个更好的答案为什么它解决了这个问题。如果下一行在下一节中,这将不起作用。他们没有为Obj C编码。Thks这对下一个有效,但是上一个索引呢?很好,这是一种非常自然的方法,可以将其作为扩展。请参阅
nextIndexPath()
上述方法的版本。我想这是一个小小的疏忽:一个节可以由零行组成。如果返回到前一节中的零行,则返回一个行=-1(numberOfRows(…)-1
)?@andreas1724:对于零行部分,不会使用indexPath,因为在这种情况下,不会为索引路径调用行。此外,这只是索引路径的帮助器。因此,零行的概念无效,因为这只是关于indexPath。最后,由于它具有可选的返回类型,因此对于row==0的情况,也不会出现问题,这是在代码第一次返回签入如果条件。@ BISHAL GIMILE:初始<代码> CurrurnIdxPosith不是问题。我同意您的说法, CurrnTrimxPosith
并没有指向零行部分。但是请考虑这种情况:第0节是一行。第1节是零行(这是有效的)。.在第2节中是一行。currentIndexPath
指向第2节中的第一行。如果尝试计算前面的单元格IndexPath,则结果将是IndexPath[1,-1]。它应该是[0,0]。
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)
}
}