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.

Introducing the Rental Manager application<br />

31<br />

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

numberOfRowsInSection:(NSInteger)section {<br />

{<br />

return 25;<br />

}<br />

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

cellForRowAtIndexPath:(NSIndexPath *)indexPath {<br />

{<br />

static NSString *CellIdentifier = @"Cell";<br />

UITableViewCell *cell = [tableView<br />

dequeueReusableCellWithIdentifier:CellIdentifier];<br />

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

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

initWithStyle:UITableViewCellStyleDefault<br />

reuseIdentifier:CellIdentifier] autorelease];<br />

}<br />

}<br />

Listing 2.1<br />

cell.textLabel.text = [NSString<br />

stringWithFormat:@"Rental Property %d", indexPath.row];<br />

NSLog(@"Rental Property %d", indexPath.row);<br />

return cell;<br />

Handling UITableView requests for the contents to display in the table<br />

Determine<br />

number of rows<br />

When the Rental Manager application runs, the UITableView control calls your<br />

tableView:numberOfRowsInSection: method to determine how many rows you want<br />

to display B. It then calls tableView:cellForRowAtIndexPath: a number of times as<br />

the user slides up and down the list to obtain the content for each visible row.<br />

Your implementation of tableView:cellForRowAtIndexPath: is made up of two<br />

steps. The first c creates a new table view cell with the UITableViewCell-<br />

StyleDefault style. This style displays a single line of large, bold text (other built-in<br />

styles replicate the layouts found in the settings or iPod music player applications).<br />

The second step d sets that line of text to the string "Rental Property %d", where %d<br />

is replaced with the index position of the current row.<br />

Press Cmd-R to rerun the application and you should see the main view displaying<br />

a list of 25 rental properties. Your challenge in this chapter is to learn how to store<br />

data in your application before expanding your tableView:cellForRowAtIndexPath:<br />

method to display some practical information about each rental property.<br />

Before moving on, take a look at tableView:cellForRowAtIndexPath: and, in particular,<br />

the last line that calls a function named NSLog. Notice that this method takes<br />

arguments similar to those of NSString’s stringWithFormat: method, which generated<br />

the string that was displayed in the table view cells onscreen.<br />

NSLog is a handy function to learn and use. It formats a string but also sends the<br />

result to the Xcode debugger console (see figure 2.3). NSLog can be a useful way to<br />

diagnose the inner workings of your application without relying on breakpoints.<br />

While the Rental Manager application is running, you can view the output from<br />

calls to NSLog by viewing the debugger console (Shift-Cmd-Y), as shown in figure 2.3.<br />

B<br />

c<br />

Create table<br />

view cell<br />

d<br />

Set the line<br />

of text

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

Saved successfully!

Ooh no, something went wrong!