【问题标题】:Conditional formatting not working with UITableView Cells条件格式不适用于 UITableView 单元格
【发布时间】: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


【解决方案1】:

解决方案是:您也必须在 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];

希望它对你有用...

【讨论】:

  • 嗨 Goppinath,我添加了这一点,但滚动时仍然得到相同的结果。
【解决方案2】:

解决方案是我在 if 语句中比较了两个 NSnumber 对象。我将
if (carDetails.mileage > nextServiceMileageNS)更改为if ([carDetails.mileage intvalue] > [nextServiceMileageNS intvalue]),现在它可以正常工作了。随机背景的应用方式似乎是一个单元重用问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多