01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

152 CHAPTER 7 Protocols<br />

■<br />

■<br />

■<br />

UITableViewCellStyleValue1<br />

This type of cell has a black, left-aligned label and a blue, right-aligned label.<br />

This style is used commonly around the iPhone OS, specifically in the Settings<br />

application.<br />

UITableViewCellStyleValue2<br />

This type of cell is similar to a UITableViewCellStyleValue1: a black, leftaligned<br />

label and a blue, right-aligned label. In addition, this view provides a<br />

smaller gray, left-aligned text field under the primary text field. These cells are<br />

seen in the Phone application.<br />

UITableViewCellStyleSubtitle<br />

This type of cell is similar to a UITableViewCellStyleValue2, a black, leftaligned<br />

label and a smaller gray, left-aligned text field under the primary text<br />

field. The blue, right-aligned label isn’t available in this view. These cells are<br />

seen in the iPod application.<br />

While many applications use customized subclasses of UITableViewCell, the standard<br />

cells work well in many cases. The structure for the tableView:cellForRowAtIndex-<br />

Path: is similar for all applications. Apple provides special cell-view retrieval methods<br />

to help with memory management when implementing this method. Let’s look at the<br />

general structure of this method’s implementation, shown in the following listing.<br />

Listing 7.8<br />

Returning table view cells for a table view<br />

- (UITableViewCell *)tableView:(UITableView *)tableView<br />

cellForRowAtIndexPath:(NSIndexPath *)indexPath<br />

{<br />

UITableViewCell *cell;<br />

NSString *reuseID = @"cellID";<br />

Initialize<br />

tableView cell<br />

b<br />

cell = [tableView dequeueReusableCellWithIdentifier:reuseID];<br />

if(cell == nil) {<br />

True if statement<br />

cell = [[[UITableViewCell alloc]<br />

initWithStyle:UITableViewCellStyleDefault<br />

reuseIdentifier:reuseID] autorelease];<br />

NSLog(@"Made a new cell view");<br />

}<br />

}<br />

[[cell textLabel] setText:@"Hello World!"];<br />

return cell;<br />

The most important parts of this code happen on three consecutive lines. The first<br />

part B attempts to initialize the cell that’s being requested from the tableView by<br />

using the dequeueReusableCellWithIdentifier method. This method looks for a<br />

cell that has scrolled off the screen and attempts to reuse it. If it finds a cell that can be<br />

recycled, the cell is initialized; otherwise, the if statement c returns true and d is<br />

c<br />

If true, the cell<br />

is executed d

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!