samturner
samturner

Reputation: 2223

Accessing the text of a cell in a UITableView

Is there a way to access a given cell in a table?

For example, if I wanted to access the detail text of only the fourth cell in the table, how do I go about that? Is this mainly done using tags? Thanks!

Upvotes: 0

Views: 161

Answers (3)

Abizern
Abizern

Reputation: 150755

I don't believe the answers to this question!

Yes you can get the text of the cell by asking the cell. But really? What if the cell is not visible? What if the user is scrolling and the cell goes off the screen?

You have everything you need in the table view's datasource.

Look at this diagram taken from Apple's Core Competencies

enter image description here

By asking the view for the text from a cell you are breaking MVC. You are supposed to ask the model (the datasource, in this case)

Upvotes: 2

Leena
Leena

Reputation: 2676

Try this:-

   NSIndexPath *path = [NSIndexPath indexPathForRow:3 inSection:0]; 
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:path]; 

Upvotes: -1

Jack Humphries
Jack Humphries

Reputation: 13267

UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
NSString *detail = cell.detailTextLabel.text

Change indexPathForRow to the appropriate row number.

Upvotes: 2

Related Questions