Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data between UITableViewCell and UITableViewController?

I created master details template project in xcode 4.6 and I added custom cell with 2 textfields. I also created new class which is subclass of UITableViewCell and inside this class I created outlets for text fields. When user types something NSMutableArray is updated and this works fine. Now I am wondering how to pass this array back to MasterViewController (UITableViewController) so that I can use this data to show calculations.

I tried using tutorials for delegates between UIViewControllers but I keep getting errors. Any help is appreciated.

like image 385
James Douglas Avatar asked Jan 31 '26 03:01

James Douglas


1 Answers

You shouldn't keep data inside the UITableViewCell, as it breaks the MVC.

You need to get a reference of the UITextField on your cell. This is how I do in a login form:

I have a custom cell subclass called TextFieldCell, it has an outlet called textField, I want that my UITableViewController have references to these UITextFields.

First I open my storyboard, set the cell class to TextFieldCell and than connect the UITextField to cell textField outlet. Than I add this to the tableView:cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    (…)
    if (indexPath.row == 0) {
        // Sets the textField of the first cell as the loginTextField.
        self.loginTextField = tCell.textField;
    } else {
        // Sets the textField of the second cell as the passwordTextField.
        self.passwordTextField = tCell.textField;
    }
    tCell.textField.delegate = self;
    (…)
}

Now I can access the value of my loginTextField and my passwordTextField. I do that on the tableView:cellForRowAtIndexPath: because that's when I'm creating the cell to add to the table view.

like image 63
Marco Pompei Avatar answered Feb 02 '26 17:02

Marco Pompei