我有一个带有自定义 UItableViewCell
的 UItableView
。每个单元格中都有 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/