views:

33

answers:

2

Hi all,

Got a weird problem with my NSMutableArray and UITableView.

It's got 4 items in the array, the table shows 4 rows but each rows only contains the first array item, rather than row 1 showing array item 1, row 2 showing item 2, etc...

Here's my code:

My Mutable Array //in .h

NSMutableArray *tableRows;

//in .m

tableRows = [[NSMutableArray arrayWithObjects:@"For Business, For Pleasure",
                  @"A Great Decision",
                  @"Air Charter Your Honeymoon",
                  @"Time Flies", nil] retain];

My table data source and delegate has

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return (NSInteger)[tableRows count];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"CELL_AIR";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if(cell == nil) {
        cell = [self makeTableCell:cellID identifier:indexPath];
    }
    return cell;
}

-(UITableViewCell *)makeTableCell:(NSString *)identifier identifier:(NSIndexPath *)indexPath {
    //frames
    CGRect cellFrame = CGRectMake(0, 10, 320, 50);
    CGRect lbl1Frame = CGRectMake(10, 0, 320, 25);
    CGRect lbl2Frame = CGRectMake(10, 20, 320, 20);


    UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:cellFrame reuseIdentifier:identifier];

    UILabel *lbl1 = [[UILabel alloc] initWithFrame:lbl1Frame];
    lbl1.tag=1;
    lbl1.font = [UIFont systemFontOfSize:19];
    lbl1.text = [tableRows  objectAtIndex:indexPath.row];


    UILabel *lbl2 = [[UILabel alloc] initWithFrame:lbl2Frame];
    lbl2.tag=2;
    lbl2.text = @"Tap to read more...";
    lbl2.font = [UIFont systemFontOfSize:12];

    [cell.contentView addSubview:lbl1];
    [cell.contentView addSubview:lbl2];

    return cell;
}

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

Any help would be great appreciated!

Thanks

C

+2  A: 

This causes the bug:

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    return 1;
}

You have only 1 row for each section. Therefore, for every section, the index path starts at 0 again then you only get the first item in array

I think the best way to do should be:

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    return (NSInteger)[tableRows count];
}
vodkhang
Excellent. Thank you - will mark as answered.
Chris M
+1  A: 

Hi Chris,

Adding on what vodkhang have answered, this is how I would do it:

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
return [tableRows count];
}

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(cell == nil) {
    cell = [self makeTableCell:cellID identifier:indexPath];
}

[[cell textLabel]
         setText:[tableRows objectAtIndex:indexPath.row]];

return cell;
}

I dont really use (NSInteger) [array count] at all. Since the method count returns an integer type. I hope this helps.

Melvin Lai
This does help, thank you Melvin. :)
Chris M