bmueller
bmueller

Reputation: 2701

UITableView Cells All Repeat 1st Cell

I have a UITableView that was working fine until I added sections to it - now the data from the first cell repeats throughout all the other cells. When I remove the sections line below, everything goes back to normal.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger sections = self.objects.count;

    return sections;
}

Any ideas on what could be going on here? My data source is Core Data, by the way.

EDIT

For numberOfRows I'm returning 1.

Here's how I'm implementing the cells:

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {

Item *p = [[[ItemStore defaultStore] allItems]
                                objectAtIndex:[indexPath row]];

    ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemCell"];

    [cell setController:self];
    [cell setTableView:tableView];

    [[cell nameLabel] setText:[p itemName]];
    [[cell serialNumberLabel] setText:[p serialNumber]];
    [[cell valueLabel] setText:[NSString stringWithFormat:@"$%d", [p valueInDollars]]];

    [[cell thumbnailView] setImage:[p thumbnail]];


return cell;
}

Upvotes: 0

Views: 615

Answers (2)

glenn sayers
glenn sayers

Reputation: 2414

You are probably using

cell.textlabel.text = [self.objects objectAtIndex: indexPath.row];

in your cellForRowAtIndexPath method.

This would work in a table view with one section, as the row number will increase each time. However, if you have multiple sections, the row number is reset back to 0 for the first row in each section. Therefore, indexPath.row will always return 0 in the cellforRowAtIndexPath method, hence you are getting the first object each time. You will have to do a check for which section you are in, or even use the section number as a way of getting the correct object from the array.

EDIT: Looking at the code you've posted, this is exactly what's happening! Look into identifying which section you're in, and filling it appropriately.

Upvotes: 2

Thomas Clayson
Thomas Clayson

Reputation: 29925

This usually happens because of the way that the cells are reused in iOS.

Make sure you're setting all the available options within your cellForRowAtIndexPath: method:

// init cell etc...
cell.textLabel.text = @"Your dynamic text";
cell.textLabel.textColor = [UIColor myColor];
// etc...

return cell;

Also as you're using core data make sure that you're selecting the right data from the NSArrays and NSDictionaries you may be using.

If this doesn't help please add some more info, including some code etc.

Upvotes: 1

Related Questions