user1008723
user1008723

Reputation: 23

TableView: How to select another row's value from didSelectRowAtIndexPath routine

When didSelectRowAtIndexPath is called, it's easy to get a cell's text value using: UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; entryText = cell.textLabel.text;

However, I'm having a problem figuring out how to get the cell text value from another row at the same time. For instance, if a user clicks on row 0, the above will get me the cell text from row 0. But I need to get the cell text from row 1 and row 2.

How do I do that?

Upvotes: 2

Views: 2114

Answers (3)

simongking
simongking

Reputation: 740

If you create your own IndexPath variable for those cells, for example:

NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
UITableViewCell *cell1 = [self.tableView cellForRowAtIndexPath:indexPath1];
NSString *cell1Text = [NSString stringWithString: cell1.textLabel.text];

NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:indexPath.row+2 inSection:indexPath.section];
UITableViewCell *cell2 = [self.tableView cellForRowAtIndexPath:indexPath2];
NSString *cell2Text = [NSString stringWithString: cell2.textLabel.text];

That should do the trick.

Upvotes: 0

ennuikiller
ennuikiller

Reputation: 46965

It really depends on how you are storing the datasource for the table. If it's in an array, then you simply index into the array to get the value.

Upvotes: 0

Ole Begemann
Ole Begemann

Reputation: 135550

Simply ask your model for the data. You should never use views to store data. This is especially important for table view cells where the data in the view can be gone from one moment to the next as the user scrolls a cell off the screen.

Upvotes: 3

Related Questions