【问题标题】:Get the row of UITextField inside UITableViewCell获取 UITableViewCell 中的 UITextField 行
【发布时间】:2013-10-24 14:07:05
【问题描述】:

我在 UITableView 的自定义单元格中有两个 UITextFields。 我需要编辑和存储文本字段的值。 当我在UITextField 内部单击时,我必须知道它所属的行才能将值保存到本地数组的正确记录中。 如何获取 textField 的行索引? 我试过了:

-(void)textFieldDidBeginEditing:(UITextField *)textField
{

     currentRow = [self.tableView indexPathForSelectedRow].row;


}

但是当我在 UITextFieldRow 内部单击时 currentRow 不会改变。只有当我单击(选择)整行时才会改变...

【问题讨论】:

    标签: ios uitableview uitextfield


    【解决方案1】:

    我要做的是创建一个自定义单元格,然后将我需要的任何自定义 UI 元素放入其中并创建一个属性 indexPath,该属性会在单元格出列时设置。然后我将 indexPath 传递给didSet 中的自定义元素。

    class EditableTableViewCell: UITableViewCell {
    
        @IBOutlet weak var textField: TableViewTextField!
    
        var indexPath: IndexPath? {
           didSet {
               //pass it along to the custom textField
               textField.indexPath = indexPath
            }
        }
    }
    
    
    class TableViewTextField: UITextField {
         var indexPath: IndexPath?
    }
    

    在TableView:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "EditableCell") as! EditableTableViewCell
            cell.indexPath = indexPath
            return cell
    }
    

    然后我实现UITextFieldDelegate 协议,并且由于 textField 有它的 indexPath,您将始终知道它来自哪里。 不确定设置委托的最佳位置在哪里。最简单的方法是在单元出列时设置它。

    override func textFieldDidEndEditing(_ textField: UITextField) {
        guard let myTextField = textField as? TableViewTextField else { fatalError() }
        guard let indexPath = myTextField.indexPath else { fatalError() }
     }
    

    【讨论】:

      【解决方案2】:

      在 iOS 8 中,我发现模拟器和设备有不同数量的 superview,所以这更通用一点,应该适用于所有版本的 iOS:

      UIView *superview = textField.superview;
      while (![superview isMemberOfClass:[UITableViewCell class]]) { // If you have a custom class change it here
          superview = superview.superview;
      }
      
      UITableViewCell *cell =(UITableViewCell *) superview;
      NSIndexPath *indexPath = [self.table indexPathForCell:cell];
      

      【讨论】:

        【解决方案3】:

        文本字段没有向表格视图发送触摸事件,因此 indexPathForSelectedRow 不起作用。您可以使用:

        CGPoint textFieldOrigin = [self.tableView convertPoint:textField.bounds.origin fromView:textField];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:textFieldOrigin]; 
        

        【讨论】:

        • 我猜这是一个强大的解决方案。接受的答案具有超级视图层次结构的依赖性,并且在该操作系统中是特定的,所以现在它对我来说是不好的解决方案。
        【解决方案4】:

        1>您可以通过在 CellForRowAtIndexPath 中以编程方式创建文本字段并将文本字段的标记设置为 indexpath.row 来实现。 然后 textFieldDidBeginEditing 你可以获取 textField.tag 并实现你想要的。

        2>另一种方法是在一个表格视图中包含 2 个自定义单元格。这样,您可以单独放置文本字段并从实用程序面板设置它们的标签。

        【讨论】:

        • 不,因为我有2s。
        • 我知道您有 2 个文本字段。而是直接将文本字段带入您的单元格。只需以编程方式创建它们。
        • 你有多少个文本域与确定你在哪一行无关,让所有文本域与行有相同的标签
        • 他需要单独识别文本字段,这就是为什么我建议他在创建 CellForRowAtIndexPath 后标记文本字段并使用标签识别 textFieldDidBeginEditing 中的文本字段。
        【解决方案5】:

        试试这个

        //For ios 7
        
        UITableViewCell *cell =(UITableViewCell *) textField.superview.superview.superview;
        NSIndexPath *indexPath = [tblView indexPathForCell:cell];
        
        
        //For ios 6
        
        UITableViewCell *cell =(UITableViewCell *) textField.superview.superview;
        NSIndexPath *indexPath = [tblView indexPathForCell:cell];
        

        【讨论】:

        • 为什么是3次superview?
        • tableviewCells 的层次结构发生了变化所以添加了一个额外的.superview
        • 您能否指出解释此更改的文档?问候
        • 虽然这种方法有效,但它注定会失败,并且当苹果做出改变时只会导致问题......
        猜你喜欢
        • 1970-01-01
        • 2014-07-27
        • 1970-01-01
        • 2012-03-23
        • 2011-09-19
        • 1970-01-01
        相关资源
        最近更新 更多