Managing Delegate of UITextFields in Custom UITableViewCell

后端 未结 5 1557
长情又很酷
长情又很酷 2021-01-17 13:05

So I have looked around a quite a bit, and nothing on here seems to explain exactly the correct way of doing this. I have 7 UITextFields within a custom UITableViewCell.

相关标签:
5条回答
  • 2021-01-17 13:49

    You shouldn't have a problem setting the delegate of the cell's text field to be your view controller.

    This is what you need to do:

    1) The view controller needs to implement the UITextFieldDelegate protocol

    2) Declare a property for the text field in your custom cell

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

    3) Then set the view controller as the text field's delegate in the method cellForRowAtIndexPath

    - (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;
    }
    
    0 讨论(0)
  • 2021-01-17 13:51

    My suggestion would be to "tag" (i.e. set the tag) of each textField with a value that encodes the section, row, and one-of-7 text views in the table, then make the UIViewController the delegate.

    So you need to bound the size of these - say you will never have more than 100 rows. So you encode this as:

    .tag = 1000*section + 100*row +

    When you get a message you can have a method/function take the tag and decode it into section, row, tag, and do what you need to have done.

    0 讨论(0)
  • 2021-01-17 13:54

    In my opinion the cell should manage the keyboard since it is the one that is holding the UITextField. You can set your cell as the UITextField delegate. In my own application, I have done this and then made my cell have it's own delegate. Any of the methods of UITextField or any new methods that should be handled by the controller can be passed along to the controller through the cells delegate.

    In this way the cell can still be generic without knowing anything about what the application is actually doing.

    0 讨论(0)
  • 2021-01-17 13:55

    To declare your TableViewController as the delegate include <UITextFieldDelegate> at the end of your @interface in the TableViewController's .h file.

    @interface MyTableViewController : UITableViewController <UITextFieldDelegate>
    

    Then, connect the text fields by ctrl-dragging each field under the @interface. Each UITextField is connected to its respective property by an IBOutlet. Finally, in the .m file include the following function to show the delegate which field you want to return....

    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        [aTextField resignFirstResponder];
        return YES;
    }
    
    0 讨论(0)
  • 2021-01-17 14:01

    Swift version (Based on Eyal's answer)

    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;
        ....
    }
    
    0 讨论(0)
提交回复
热议问题