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;
}
You are probably using
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.