Bourne
Bourne

Reputation: 10302

Fetching invisible uitableviewcell

I have a tableview with each cell loaded from a xib. It is in the format UILabel some space and UITextField. On a button click somewhere on the view I need the action to fetch all the textfield values in the UITableView with the labels in a dictionary. The problem is when I alloc a cell and fetch the values from there using indexPath, the code crashes exactly at the indexPath of the cell which is invisible (out of screen bounds).

I would appreciate any good ideas on how to fetch those cells which are not visible on screen.

Thanks!

Upvotes: 3

Views: 2405

Answers (1)

Erik Asmussen
Erik Asmussen

Reputation: 231

UITableViewCells are reused when they scroll out of view, so that's probably why it won't let you access attributes of UITableViewCells that are not visible.

An NSDictionary might be your best bet:

  1. Set a tag for each cell's UITextField based on the indexPath.row value during cell creation.
  2. Set the delegate for each UITextField to your view controller.
  3. In the delegate method textFieldDidEndEditing, update your dictionary as such: [dictionary setObject:textField.text forKey:[NSNumber numberWithInt:textField.tag]]

And now you can access an array of your textField values with [dictionary allValues].

Upvotes: 4

Related Questions