【发布时间】:2014-05-28 14:34:37
【问题描述】:
我认为这是重复使用单元格的问题,但我无法弄清楚这一点,希望能多多关注它。我有一个 uitableviewcell 子类,它比较两个值,如果一个值更高,它将单元格背景更改为红色,否则将其更改为白色。当我滚动时,一些单元格是白色的,应该是红色的,反之亦然。
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
tut_MaintListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"maintCell" forIndexPath:indexPath];
// Configure the cell...
MaintItem *mItem = [self.fetchedResultsController objectAtIndexPath:indexPath];
[cell configureCellForEntry:mItem sendCar:self.carDetail];
return cell;
}
UITableViewCell 子类
- (void)configureCellForEntry:(MaintItem *)mItem sendCar:(Car *)carDetails
{
self.itemLabel.text = [mItem valueForKey:@"item"];
self.actionLabel.text = [mItem valueForKey:@"action"];
self.engineLabel.text = [mItem valueForKey:@"engineCode"];
self.maintIDLabel.text = [[mItem valueForKey:@"maintID" ]stringValue];
// Grab the mileages recorded in the log for this maint item and turn it into a sorted array
NSArray *result = [[mItem.toLog valueForKey:@"mileage"] sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"" ascending:YES]]];
// Determine mileage of next service
NSString *nextServiceMileage = [NSString stringWithFormat:@"%d", [mItem.intMileage intValue] + [[result lastObject] intValue]];
nextServiceMileageNS = @([nextServiceMileage intValue]);
if ([mItem.frequencyID isEqualToNumber:[NSNumber numberWithInt:3]])
{
NSString *timing = [[NSString alloc] initWithFormat:@" %@ Once at %@ miles or %@ months", [mItem valueForKeyPath:@"frequencyID"], [mItem valueForKeyPath:@"intMileage"], [mItem valueForKeyPath:@"intMonth"]];
NSString *howOften = [[NSString alloc] initWithFormat:@" %@", timing];
self.howOftenLabel.text = howOften;
if (carDetails.mileage > nextServiceMileageNS)
{
self.backgroundColor = [UIColor redColor];
}
else
{
self.backgroundColor = [UIColor whiteColor];
}
}
else if ([mItem.frequencyID isEqualToNumber:[NSNumber numberWithInt:4]])
{
NSString *timing = [[NSString alloc] initWithFormat:@" %@ Every %@ miles or %@ months, due at %@ ", [mItem valueForKeyPath:@"frequencyID"], [mItem valueForKeyPath:@"intMileage"], [mItem valueForKeyPath:@"intMonth"], nextServiceMileage];
NSString *howOften = [[NSString alloc] initWithFormat:@" %@", timing];
self.howOftenLabel.text = howOften;
if (carDetails.mileage > nextServiceMileageNS)
{
self.backgroundColor = [UIColor redColor];
}
else
{
self.backgroundColor = [UIColor whiteColor];
}
}
else
{
NSString *howOften = [[NSString alloc] initWithFormat:@" %@", [mItem valueForKeyPath:@"frequencyID"]];
self.howOftenLabel.text = howOften;
}
}
【问题讨论】:
-
您的单元格必须具有背景颜色的默认值,并且仅在特定条件下更改颜色。否则在上下滚动时,单元格的顺序会混淆。
-
self.backgroundColor = [UIColor whiteColor];是您的配置单元格的第一行输入方法,然后重试 -
嗨,Panayot,我刚刚尝试过,但仍然得到相同的背景颜色不正确的结果。
标签: ios objective-c uitableview