我认为这是重复使用细胞的一个问题,但我无法解决这个问题,并会欣赏一些额外的眼睛。我有一个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;
}
}
答案 0 :(得分:0)
解决方案是:您还必须在else部分中设置背景颜色。更好的解决方案是,
UIView *backgroundView;
if (condition_1) {
backgroundView = [UIView new];
[backgroundView setBackgroundColor:[UIColor whiteColor]];
} else if (condition_2) {
backgroundView = [UIView new];
[backgroundView setBackgroundColor:[UIColor redColor]];
} else {
// here you can set or leave it.
}
[self setBackgroundView:backgroundView];
希望它对你有用......
答案 1 :(得分:0)
解决方案是我在if语句中比较两个NSnumber对象。我改变了
if (carDetails.mileage > nextServiceMileageNS)
到if ([carDetails.mileage intvalue] > [nextServiceMileageNS intvalue])
,现在它正常运行了。应用随机背景的方式似乎是细胞重用问题。