Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Trickerie

macrumors newbie
Original poster
Jun 23, 2010
8
0
Ok, so, I'm just beginning my study into iPhone programming, and had a pretty basic question.

Heres a section of code using table views I just can't get my head around:

Code:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

NSString *name = [[contacts objectAtIndex: indexPath.row] valueForKey: @"Name"];
	
UILabel *label = [cell textLabel];
	
label.text = name;

My question may be pretty basic, but why exactly does that change cell's text?

Any help would be great! Thank you!
 
Ok, so, I'm just beginning my study into iPhone programming, and had a pretty basic question.

Heres a section of code using table views I just can't get my head around:

Code:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

NSString *name = [[contacts objectAtIndex: indexPath.row] valueForKey: @"Name"];
	
UILabel *label = [cell textLabel];
	
label.text = name;

My question may be pretty basic, but why exactly does that change cell's text?

Any help would be great! Thank you!

Well, it gets a reference to the cell, and then the cell's label, and the label.text = name is the same as [label setValue:name forKey:mad:"text"]... which is why the cell changes. Seems like a lot of effort to update a cell value, but I'm pretty new to Cocoa myself.
 
That was what I was thinking myself, but I just wanted to be sure.

The long-way-around way of changing a cell is probably done because the sample code is from a book I'm reading. Its actually a really good book, though, for beginners!

Its called:

iPhone for Programmers: An App Driven Approach, from the Deitel Developer Series.
 
It could just as easily be written:
Code:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

NSString *name = [[contacts objectAtIndex: indexPath.row] valueForKey: @"Name"];
	
cell.textLabel.text = name;
The reason for keeping the cell reference in a variable is that it is commonly needed for other things within the method, like instantiating a cell if one wasn't able to be dequeued. As for name, having a reference to it allows you to check, via debugging, if it was set properly before using it in the cell.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.