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.

Completing Rental Manager v1.0, App Store here we come!<br />

53<br />

Having modified RootViewController.h to specify the new data types related to<br />

storing rental property details, you’re ready to declare details about an initial set of<br />

rental properties. Open up RootViewController.m and insert the contents of the following<br />

listing directly below the line #import "RootViewController.h".<br />

Listing 2.4<br />

RootViewController.m<br />

#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))<br />

RentalProperty properties[] = {<br />

{ @"13 Waverly Crescent, Sumner", TownHouse, 420.0f },<br />

{ @"74 Roberson Lane, Christchurch", Unit, 365.0f },<br />

{ @"17 Kipling Street, Riccarton", Unit, 275.9f },<br />

{ @"4 Everglade Ridge, Sumner", Mansion, 1500.0f },<br />

{ @"19 Islington Road, Clifton", Mansion, 2000.0f }<br />

};<br />

The main portion of listing 2.4 declares a variable called properties. This is an array<br />

of RentalProperty structures. The array is initialized with details of five example<br />

properties in your portfolio by using a combination of the array and structure initialization<br />

syntaxes discussed earlier in this chapter.<br />

Now all that’s left to do is to provide suitable tableView:numberOfRowsInSection:<br />

and tableView:cellForRowAtIndexPath: replacements that refer back to the data in<br />

the properties array to satisfy their requests. These can be found in the following listing.<br />

Listing 2.5<br />

RootViewController.m<br />

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

numberOfRowsInSection:(NSInteger)section {<br />

{<br />

return ARRAY_SIZE(properties);<br />

}<br />

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

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

{<br />

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

UITableView *cell =<br />

[tableView dequeueReusableCellWithIdentifier:CellIdentifier];<br />

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

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

initWithStyle:UITableViewCellStyleSubtitle<br />

reuseIdentifier:CellIdentifier] autorelease];<br />

}<br />

cell.textLabel.text = properties[indexPath.row].address;<br />

cell.detailTextLabel.text =<br />

[NSString stringWithFormat:@"Rents for $%0.2f per week",<br />

properties[indexPath.row].weeklyRentalPrice];<br />

return cell;<br />

}

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

Saved successfully!

Ooh no, something went wrong!