ios - 移至自定义 UItableViewCell 中的下一个 UItextfield(具有多个部分的 Tableview)

标签 ios swift uitableview swift3 swift4

我有一个带有自定义 UItableViewCellUItableView。每个单元格中都有 UItextField。现在我希望用户能够通过点击键盘上的下一个按钮移动到下一个文本字段。 我已经知道我们可以在 cellForRowAtIndexPath

中执行以下操作
cell.textField.tag =  indexPath.row

然后移动到下一个字段

if let nextField = self.view.viewWithTag(textField.tag + 1) as? UITextView {
            nextField.becomeFirstResponder()
        } else {
            textField.resignFirstResponder()
        }

但在我的例子中,我们在 UItableview 中有多个部分,所以这个

cell.textField.tag = indexPath.row

不会工作。有线索吗?提前致谢。

最佳答案

你也可以这样做 1)通过将文本框的标签作为部分和行的组合

  textfield.tag = indexPath.section*1000 + indexPath.row

并通过代码获取:

let Row = textfield.tag % 1000
let Section = (textfield.tag - Row)/1000 

或者 2)您可以通过在任何文本字段的委托(delegate)方法中使用以下代码来获取文本字段所在的 TableView 单元格的部分和行值:

let item:UITableViewCell = textField.superview?.superview as! UITableViewCell

 let indexPath = table_view.indexPath(for: item)

 print(" Section :\(indexPath?.section)")
 print(" Row :\(indexPath?.row)")

检查部分和行值,然后根据需要的条件设置文本字段的 becomeFirstResponder(根据您的部分和行数)

关于ios - 移至自定义 UItableViewCell 中的下一个 UItextfield(具有多个部分的 Tableview),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49608981/

相关文章:

ios - UINavigationController 在 iOS6 中崩溃

iphone - 在 UIWebView 中添加 UIButton

ios - 如何在 iOS ViewCellRenderer 中更改 Forms ViewList 的 MenuItem 颜色?

ios - awakeAfterUsingCoder() 导致 EXC_BAD_ACCESS

swift - CollectionView 委托(delegate)错误

ios - Xamarin TableView 崩溃

ios - Ionic run 无法部署到设备

ios - 在 iOS 纵向模式下维护一个 View Controller ,在横向模式下维护另一个 View Controller

ios - 快速更新 TableView 失败

带有图像和复选标记的 iOS 自定义 TableView