0

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:

Day 1 Monday

Day 2 Tuesday

PerroVerde
  • 49
  • 8

2 Answers2

0

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"];
Ponf
  • 1,190
  • 1
  • 12
  • 28
  • Seems that it worked, I'm not having repeated content anymore but experimenting other problems with cells displaying wrong info. But it could be anything not related with this. So thanks!, I'll accept the answer as correct. – PerroVerde Jul 03 '15 at 09:42
0

First thing, create CalendarDayCell *cell in .h file and reuse cell object. Second thing, set Nil before setting new value in cellForRowAtIndexPath. For example set

cell.label.text = @"";

cell.imageview.image = Nil;

Then set your desired text. Same with didSelectRowAtIndexPath, before setting new color, first set clear color like:

cell.container.backgroundColor = [UIColor clearColor];

cell.container.backgroundColor = bgColor;

It may help you.

Community
  • 1
  • 1
Bhanupriya
  • 1,202
  • 1
  • 9
  • 20