Jay_Ayres
Jay_Ayres

Reputation: 48

How do I get information from a UITableViewCell?

I have a TableView with a prototype cell, in this prototype cell I have a text field. Well, what I need is to get a information from this text field when it changes. I need to do that to feed a object.

How can I do that ?

Upvotes: 0

Views: 80

Answers (2)

John
John

Reputation: 2694

You need a custom cell with a public property (IBOutlet) UITextfield. Then you can set your ViewController as the textField's delegate in cellForRowAtIndexPath,

     cell.textField.delegate = self; 

in this case your ViewController has to implement UITextFieldDelegate protocol.

Upvotes: 0

Fogmeister
Fogmeister

Reputation: 77631

Short answer - you don't.

Slightly more in depth answer...

The UITableViewCell is a "view". It is used only to display information to the screen. It is not for storing data in.

In a UITableViewController (i.e. UITableViewDatasource and UITableViewDelegate) you should have an NSArray (or an NSFetchedResultsController) that you use to store information in.

Then using the delegate methods you populate the table with that data.

If the data changes then you reload the table to update the cells.

What should never happen is the following...

  1. Load the table by passing in data to the cell.
  2. The cell then changes its own data and changes what is on the screen.
  3. The controller then reads that data out of the cell.
  4. No, no, no, don't do this.

What should happen is this...

  1. Load the table and configure the cell display to represent the correct part of the data.
  2. When the button is pressed (or text field is changed, etc...) in the cell then call a method back in the controller to update the data accordingly.
  3. Now the data has changed, reload the cell.
  4. It will now show the correct information based on the new data.

Upvotes: 5

Related Questions