iVela
iVela

Reputation: 1201

How can I get the value of a UITableViewCell that is not visible?

I have a table view cell that has many rows with a UITextView. In those UITextView the user can enter values.

The problem that I have is that if I want to get the value of a row when the row is hidden I get nil.

NSString *cellValue = ((UITextField *)[[[self.table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]] contentView] viewWithTag:[self.table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]].tag]).text;

I was thinking to save the value that the user enters in the UITextField, but if the user clicks a row that performs the calculate the delegate textFieldDidEndEditing is not being called.

I don't know how can I get this values in a secure way.

Upvotes: 3

Views: 2072

Answers (4)

Yonathan Goriachnick
Yonathan Goriachnick

Reputation: 201

You can access rows that are not currently displayed by directly referring the tableView's dataSource with the wanted IndexPath.

You can achieve this by doing something like this (when using Swift 3):

UITableViewCell cell = self.tableView(self.tableView, cellForRowAt: indexPath)

Upvotes: 1

FluffulousChimp
FluffulousChimp

Reputation: 9185

As mentioned, you would generally use the UITableView data source to access this.

That said, you are probably not seeing the data you expect because the you are using dequeued cells. If the number of cells is small, you could consider caching your own cells in an NSDictionary whose keys are some combination of the section and row, and accessing their view hierarchy via your own cache.

Upvotes: 1

Jaybit
Jaybit

Reputation: 1864

What is happening most likely is your reusing cells. So when your cell with the textfield goes off screen it gets reused so you cannot access it anymore. What you will need to do is grab the value out the the textfield right after they enter it and store it somewhere. You can use – textField:shouldChangeCharactersInRange:replacementString: so that when ever the user enters a character into the textfield you can save the string.

Upvotes: 0

MystikSpiral
MystikSpiral

Reputation: 5028

You should target the data source that is behind the UITableView. The UITableView is just a display mechanism and not the primary resource for getting to data. You can get the index for the cell based on the title, but then I would look tot he data source to get your actual data. Better yet I would just handle all operations against the data source itself if that is possible. When you build the table initially your data source is a list or array of some kind I am sure so you can just use it. You may need to create a variable on the view and retain the list/array there to make sure you have it and you should be all set.

Good Luck!

Upvotes: 5

Related Questions