Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Table View programming - simulator displays first and last item in dummy array only

I am working on a project that involves configuring table view cells.

I have set up a "dummy data store" in FPLTableViewController.m, within the viewDidLoad method as such:

FPLFlightEvent *flight1 = [[FPLFlightEvent alloc] init];
    flight1.departureAirport = @"KRNM";
    flight1.arrivalAirport = @"KNYL";
    flight1.totalFlightTime = @1.1;
    flight1.flightDate = @"12 Feb 2013";

    FPLFlightEvent *flight2 = [[FPLFlightEvent alloc] init];
    flight2.departureAirport = @"KNYL";
    flight2.arrivalAirport = @"KRNM";
    flight2.totalFlightTime = @0.9;
    flight2.flightDate = @"13 Feb 2013";

    FPLFlightEvent *flight3 = [[FPLFlightEvent alloc] init];
    flight2.departureAirport = @"KRNM";
    flight2.arrivalAirport = @"KVNY";
    flight2.totalFlightTime = @1.1;
    flight2.flightDate = @"14 Feb 2013";

    FPLFlightEvent *flight4 = [[FPLFlightEvent alloc] init];
    flight2.departureAirport = @"KVNY";
    flight2.arrivalAirport = @"KRNM";
    flight2.totalFlightTime = @1.0;
    flight2.flightDate = @"15 Feb 2013";

FPLFlightEvent is a custom class with 4 properties as depicted. I create a total of four variables *flight1 through *flight4 all with similar data.

I then initialize the array for display in the table view:

self.flightEvents = [NSMutableArray arrayWithObjects:flight1, flight2, flight3, flight4, nil];

I have set numberOfSectionsInTableView to return 1.

Here is numberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.flightEvents.count;
}

Here is cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"LogbookPrototypeCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    FPLFlightEvent *flightEvent = [self.flightEvents objectAtIndex:indexPath.row];
    cell.textLabel.text = flightEvent.flightDate;

    return cell;
}

Any ideas why the sim is only displaying the first and last objects in the "dummy data" array?

do you have 4 rows (first has text, second is blank, third is blank, fourth has text)?

please post the full code where you initialize all four of your flight events

3 Answers

FPLFlightEvent *flight3 = [[FPLFlightEvent alloc] init];
    flight2.departureAirport = @"KRNM";
    flight2.arrivalAirport = @"KVNY";
    flight2.totalFlightTime = @1.1;
    flight2.flightDate = @"14 Feb 2013";

    FPLFlightEvent *flight4 = [[FPLFlightEvent alloc] init];
    flight2.departureAirport = @"KVNY";
    flight2.arrivalAirport = @"KRNM";
    flight2.totalFlightTime = @1.0;
    flight2.flightDate = @"15 Feb 2013";

you are just setting the properties for flight2 over and over again. you need to change it to something like this

FPLFlightEvent *flight3 = [[FPLFlightEvent alloc] init];
    flight3.departureAirport = @"KRNM";
    flight3.arrivalAirport = @"KVNY";
    flight3.totalFlightTime = @1.1;
    flight3.flightDate = @"14 Feb 2013";

    FPLFlightEvent *flight4 = [[FPLFlightEvent alloc] init];
    flight4.departureAirport = @"KVNY";
    flight4.arrivalAirport = @"KRNM";
    flight4.totalFlightTime = @1.0;
    flight4.flightDate = @"15 Feb 2013";

Wow, that was fast. I will edit the post to include the relevant code. Sorry, trying to save space I guess.

Aargh. syntax. Sigh. Thanks for the help nice job.