【问题标题】:Move to next UItextfield in custom UItableViewCell (Tableview with multiple section)移动到自定义 UItableViewCell 中的下一个 UItextfield(具有多个部分的 Tableview)
【发布时间】:2018-09-11 12:50:11
【问题描述】:

我有一个带有自定义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

行不通。有什么线索吗?提前致谢。

【问题讨论】:

  • 如果您遵循此答案stackoverflow.com/questions/31766896/…,则将 UITextView 更改为 UITextField
  • 我试过了。但没有帮助
  • self.view.viewWithTag(textField.tag + 1) 这是错误的。尝试从它的superview 获取textField,这可能不是self.view
  • @TheTiger 我的问题不是关于 self.view.viewWithTag(textField.tag + 1)。它关于为分组表视图中的单元格分配标签
  • @sardarasad 检查更新的答案。

标签: ios swift uitableview swift3 swift4


【解决方案1】:

你也可以 1)通过将文本字段的标签作为部分和行的组合

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

并使用打击代码获取它:

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

或 2)您可以通过在任何文本字段的委托方法中使用以下代码来获取文本字段所在的 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(根据您的部分和行数)

【讨论】:

    【解决方案2】:

    感谢大家的回答我的错我无法理解我通过稍微调整我的数据源来做到这一点

      func getBaseValueCount(currentSection:Int) -> Int {
        var count = 0
        if currentSection == 0 {
            return count
        }
        else{
            for i in 0..<currentSection {
                count = count + (self.formSections[i].fields?.count)!
            }
        }
    
        return count
    }
    

    然后在 cellForRowAtIndexPath 中

     cell.txtAnswer.tag = self.getBaseValueCount(currentSection: indexPath.section) + indexPath.row
    

    再次感谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-18
      • 1970-01-01
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-29
      • 2012-03-02
      相关资源
      最近更新 更多