I have an *UITableView
that show events in a period of time, some kind of calendar. Each section is a day of the week and each event is a custom *UITableViewCell
. The events are inside of an *NSArray
, and depending on their date and time, those events are assigned to one section of the table.
The number of rows in section is fine, but when cells are displayed some of them start to repeat their content in other cells of other sections overwriting their info. Also this cells have an assigned color, and on the didSelectRowAtIndexPath
method their background change to that color when clicked but the same as above happens, other cells of other sections also update their background color.
I think that is somewhat related with the cells reuse, but I can't find what I'm doing wrong.
PD: I'm new to iOS, so please feel free to ask for more code, to correct me, etc. Thanks in advice.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CalendarDayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CalendarDayCell"];
if (cell == nil) {
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"CalendarDayCell" owner:self options:nil];
cell = [nibs objectAtIndex:0];
}
CalendarEvent *ce = self.events[indexPath.row];
NSString *initHr = [self formatDateGetHour: ce.timestart];
... sets cell info ...
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
UIColor *bgColor = [self lighterColorForColor:[self colorWithHexString:ce.color]];
CalendarDayCell *cell = (CalendarDayCell *)[_calendarTable cellForRowAtIndexPath:indexPath];
cell.container.backgroundColor = bgColor;
...
}
Here's how I make the calc for rows in sections
- (void) calculateRowsInSections
{
mondayEventsCount = 0, tuesdayEventsCount = 0, wednesdayEventsCount = 0, thursdayEventsCount = 0, fridayEventsCount = 0, saturdayEventsCount = 0, sundayEventsCount = 0;
if (self.events.count) {
for (CalendarEvent* ce in self.events) {
if ([self getDayOfWeek : ce.timestart] == 2) mondayEventsCount++;
else if ([self getDayOfWeek : ce.timestart] == 3) tuesdayEventsCount++;
else if ([self getDayOfWeek : ce.timestart] == 4) wednesdayEventsCount++;
else if ([self getDayOfWeek : ce.timestart] == 5) thursdayEventsCount++;
else if ([self getDayOfWeek : ce.timestart] == 6) fridayEventsCount++;
else if ([self getDayOfWeek : ce.timestart] == 7) saturdayEventsCount++;
else if ([self getDayOfWeek : ce.timestart] == 1) sundayEventsCount++;
}
}
}
Some images of what is happening:
It looks like you should implement -prepareForReuse
method in your custom cell class and cleanup cell's content there. https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/occ/instm/UITableViewCell/prepareForReuse
Also instead of
if (cell == nil) {
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"CalendarDayCell" owner:self options:nil];
cell = [nibs objectAtIndex:0];
}
is better to register cell at -viewDidLoad
method:
[self.tableView registerNib:[UINib nibWithNibName:@"CalendarDayCell" bundle:nil] forCellReuseIdentifier:@"CalendarDayCell"];