【问题标题】:Managing Delegate of UITextFields in Custom UITableViewCell在自定义 UITableViewCell 中管理 UITextFields 的委托
【发布时间】:2012-07-15 20:32:55
【问题描述】:

所以我环顾四周,这里似乎没有任何东西可以准确地解释这样做的正确方法。我在自定义 UITableViewCell 中有 7 个 UITextField。

我的问题是:管理这些 UITextFields 的代表的正确方法是什么?

由于自定义单元格在技术上是项目“模型”部分的一部分,我宁愿让控制 UITableView 的控制器也控制表格单元格中的文本字段,但我不知道如何设置将文本字段(在 UITableViewCell 的子类中创建)委托给此视图控制器。

只使 UITableViewCell 的子类符合 UITextField 委托并管理其中的所有内容会是不好的做法吗?如果是这样,我应该怎么做呢?

谢谢,我们将不胜感激。

【问题讨论】:

    标签: iphone objective-c ios ipad uitableview


    【解决方案1】:

    我的建议是用一个值对每个 textField 进行“标记”(即设置标记),该值对表中的部分、行和 7 个文本视图之一进行编码,然后将 UIViewController 设为委托。

    所以你需要限制这些的大小 - 假设你永远不会超过 100 行。所以你把它编码为:

    .tag = 1000*section + 100*row +

    当您收到一条消息时,您可以让一个方法/函数获取标签并将其解码为部分、行、标签,然后执行您需要完成的操作。

    【讨论】:

      【解决方案2】:

      要将您的 TableViewController 声明为委托,请在 TableViewController 的 .h 文件中 @interface 的末尾添加 <UITextFieldDelegate>

      @interface MyTableViewController : UITableViewController <UITextFieldDelegate>
      

      然后,通过 ctrl 拖动 @interface 下的每个字段来连接文本字段。每个 UITextField 都通过 IBOutlet 连接到其各自的属性。最后,在 .m 文件中包含以下函数,以显示委托要返回哪个字段....

      - (BOOL)textFieldShouldReturn:(UITextField *)textField {
          [aTextField resignFirstResponder];
          return YES;
      }
      

      【讨论】:

        【解决方案3】:

        在我看来,单元格应该管理键盘,因为它是持有 UITextField 的那个。您可以将您的单元格设置为 UITextField 委托。在我自己的应用程序中,我已经这样做了,然后让我的单元格拥有它自己的代表。 UITextField 的任何方法或应由控制器处理的任何新方法都可以通过单元格委托传递给控制器​​。

        通过这种方式,单元格仍然可以是通用的,而无需知道应用程序实际在做什么。

        【讨论】:

        • 我同意。为此,只需在 UITableViewCell 实现的初始化中设置委托。例如,在 awakeFromNib 调用中,只需添加 self.YourTextField.delegate = self.
        【解决方案4】:

        将单元格文本字段的委托设置为视图控制器应该没有问题。

        这是你需要做的:

        1)视图控制器需要实现UITextFieldDelegate协议

        2) 为自定义单元格中的文本字段声明一个属性

        @property (nonatomic, retain) IBOutlet UITextField *textField;
        

        3) 然后在方法cellForRowAtIndexPath中设置view controller为text field的delegate

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            static NSString *cellIdentifier = @"Cell";
        
            MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        
            if (cell == nil) 
            {  
                // use this if you created your cell with IB
                cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil] objectAtIndex:0];   
        
                // otherwise use this  
                cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
        
        
                // now set the view controller as the text field delegate  
                cell.textField.delegate = self;
            }
        
            // configure cell...
        
            return cell;
        }
        

        【讨论】:

        • 谢谢!!为我工作
        【解决方案5】:

        Swift 版本(基于 Eyal 的回答)

        class MyViewController: UIViewController, ... , UITextFieldDelegate {
        
            @IBOutlet var activeTextField: UITextField!  //doesn't need to connect to the outlet of textfield in storyboard
        
            ....
        
            func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
            ....
        
            var cellTextField = self.view.viewWithTag(101) as? UITextField
            cellTextField!.delegate = self;
            ....
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多