不同部分中自定义 TableCells 中的重复内容
Repeated content in custom TableCells in different sections
我有一个 *UITableView
可以显示一段时间内的事件,某种日历。每个部分都是一周中的一天,每个事件都是 自定义 *UITableViewCell
。这些事件在 *NSArray
中,并且根据它们的日期和时间,这些事件被分配到 table 的一个部分。
节中的行数很好,但是当单元格显示时,其中一些开始在 其他节 的其他单元格中重复其内容,覆盖其信息。此外,此单元格具有指定的颜色,并且在 didSelectRowAtIndexPath
方法中,单击时它们的背景会更改为该颜色,但与上述相同,其他部分的其他单元格也会更新其背景颜色。
我认为这与单元格重用有些关系,但我找不到我做错了什么。
PD:我是 iOS 的新手,所以请随时索取更多代码,纠正我等等。谢谢你的建议。
- (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;
...
}
下面是我如何计算分段中的行
- (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++;
}
}
}
一些正在发生的事情的图像:
您似乎应该在自定义单元格 class 中实施 -prepareForReuse
方法并在那里清理单元格的内容。 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/occ/instm/UITableViewCell/prepareForReuse
也代替了
if (cell == nil) {
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"CalendarDayCell" owner:self options:nil];
cell = [nibs objectAtIndex:0];
}
最好在-viewDidLoad
方法注册单元格:
[self.tableView registerNib:[UINib nibWithNibName:@"CalendarDayCell" bundle:nil] forCellReuseIdentifier:@"CalendarDayCell"];
首先,在 .h 文件中创建 CalendarDayCell *cell 并重用单元格对象。
第二件事,在 cellForRowAtIndexPath 中设置新值之前设置 Nil。例如设置
cell.label.text = @"";
cell.imageview.image = Nil;
然后设置你想要的文字。与 didSelectRowAtIndexPath 相同,在设置新颜色之前,首先设置清晰颜色,如:
cell.container.backgroundColor = [UIColor clearColor];
cell.container.backgroundColor = bgColor;
可能对你有帮助。
我有一个 *UITableView
可以显示一段时间内的事件,某种日历。每个部分都是一周中的一天,每个事件都是 自定义 *UITableViewCell
。这些事件在 *NSArray
中,并且根据它们的日期和时间,这些事件被分配到 table 的一个部分。
节中的行数很好,但是当单元格显示时,其中一些开始在 其他节 的其他单元格中重复其内容,覆盖其信息。此外,此单元格具有指定的颜色,并且在 didSelectRowAtIndexPath
方法中,单击时它们的背景会更改为该颜色,但与上述相同,其他部分的其他单元格也会更新其背景颜色。
我认为这与单元格重用有些关系,但我找不到我做错了什么。
PD:我是 iOS 的新手,所以请随时索取更多代码,纠正我等等。谢谢你的建议。
- (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;
...
}
下面是我如何计算分段中的行
- (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++;
}
}
}
一些正在发生的事情的图像:
您似乎应该在自定义单元格 class 中实施 -prepareForReuse
方法并在那里清理单元格的内容。 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/occ/instm/UITableViewCell/prepareForReuse
也代替了
if (cell == nil) {
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"CalendarDayCell" owner:self options:nil];
cell = [nibs objectAtIndex:0];
}
最好在-viewDidLoad
方法注册单元格:
[self.tableView registerNib:[UINib nibWithNibName:@"CalendarDayCell" bundle:nil] forCellReuseIdentifier:@"CalendarDayCell"];
首先,在 .h 文件中创建 CalendarDayCell *cell 并重用单元格对象。 第二件事,在 cellForRowAtIndexPath 中设置新值之前设置 Nil。例如设置
cell.label.text = @"";
cell.imageview.image = Nil;
然后设置你想要的文字。与 didSelectRowAtIndexPath 相同,在设置新颜色之前,首先设置清晰颜色,如:
cell.container.backgroundColor = [UIColor clearColor];
cell.container.backgroundColor = bgColor;
可能对你有帮助。