01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Building an application, complete with bugs<br />

277<br />

14.1 Building an application, complete with bugs<br />

Let’s create an application and purposely add some errors so that you can discover,<br />

observe, and resolve them. The application isn’t overly complex, but it helps demonstrate<br />

the debugging tools at your disposal. In Xcode create a new Navigation-based<br />

application named DebugSample, and then replace its RootViewController tableview:<br />

numberOfRowsInSection: and tableView:cellForRowAtIndexPath: methods with<br />

the implementation in the following listing.<br />

Listing 14.1<br />

A sample tableview implementation with intentional bug<br />

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

numberOfRowsInSection:(NSInteger)section {<br />

}<br />

return 450;<br />

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

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

}<br />

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

UITableViewCell *cell =<br />

[tableView dequeueReusableCellWithIdentifier:CellIdentifier];<br />

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

NSLog(@"We are creating a brand new UITableViewCell...");<br />

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

initWithStyle:UITableViewCellStyleDefault<br />

reuseIdentifier:CellIdentifier] autorelease];<br />

}<br />

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

cell.textLabel.text =<br />

[[NSString stringWithFormat:@"Item %d", indexPath.row] retain];<br />

return cell;<br />

When you build and run this application, you should see a UITableView consisting of<br />

450 items. You should also notice that, as you flick through the table, it emits a log<br />

message indicating which cell is currently being configured, as shown in figure 14.1.<br />

Figure 14.1 Console output from the DebugSample application: a new log message is<br />

emitted for each cell that’s configured for the UITableView.

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

Saved successfully!

Ooh no, something went wrong!