【问题标题】:Font Color Set Incorrectly when using UITableView and Conditional使用 UITableView 和 Conditional 时字体颜色设置不正确
【发布时间】:2012-10-21 07:00:52
【问题描述】:

我有一个 UITableView 设置了一个核心数据获取结果控制器和使用情节提要创建的自定义单元格。一切正常,直到我尝试有条件地为某些单元格设置 UILabel 的字体颜色。

在情节提要中,默认情况下,每个单元格都以灰色字体显示“出发时间”。但是,如果单元格代表用户的当前位置,则字体应该是蓝色的。所以我在 configureCell 中设置了一个条件(靠近这个 sn-p 的底部):

-(void) configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    if ([cell isMemberOfClass:[SectionDividerCell class]])  {
        [self configureSectionDividerCell:(SectionDividerCell *)cell atIndexPath:indexPath];
    }
    else if ([cell isMemberOfClass:[VisitCell class]])  {
        [self configureVisitCell:(VisitCell *)cell atIndexPath:indexPath];
    }
}

-(void) configureVisitCell:(VisitCell *)cell atIndexPath:(NSIndexPath *)indexPath  {
    //Regular visit cell

    Visit *visit = [self.fetchedResultsController objectAtIndexPath:indexPath];

    //Set text content
    cell.titlePlace.text = visit.place_name;
    cell.arriveText.text = [visit getArrivalTimeDisplay:@"hh:mm a"];
    cell.leaveText.text = [visit getDepartureTimeDisplay:@"hh:mm a"];
    cell.durationText.text = visit.durationDisplay;

    //Set fonts
    [cell.durationText setFont:[UIFont fontWithName:@"Roboto-BoldCondensed" size:20.0]];
    [cell.arriveText setFont:[UIFont fontWithName:@"Roboto-Light" size:12.0]];
    [cell.titlePlace setFont:[UIFont fontWithName:@"Roboto-BoldCondensed" size:17.0]];

    //If current location, make text blue
    if (!visit.departure_time)    {
        [cell.leaveText setFont:[UIFont fontWithName:@"Roboto-Bold" size:12.0]];
        [cell.leaveText setTextColor:[UIColor blueColor]];
    }
    else    {
        [cell.leaveText setFont:[UIFont fontWithName:@"Roboto-Light" size:12.0]];

    }

    //Get Image for cell
    if (!cell.imgThumb.image)   {

        NSString *imageURL = [[docDir stringByAppendingString:@"/"] stringByAppendingString:visit.place.imageName];
        [cell.imgThumb setImage:[[UIImage alloc] initWithContentsOfFile:imageURL]];
    }

}

使用上面的代码,“当前位置”单元格的 UILabel 正确显示为蓝色,但其他一些单元格也显示为蓝色(似乎是随机的 - 发生在大约 10% 的单元格中。

如果我在 else 语句中添加此代码以将不是当前位置的单元格的颜色设置回默认值,它可以解决问题: [cell.leaveText setTextColor:[UIColor grayColor]];

但我不明白的是,为什么有些单元格的字体颜色设置不正确?

【问题讨论】:

    标签: objective-c ios cocoa-touch tableview


    【解决方案1】:

    为什么有些单元格的字体颜色设置不正确?

    没有错。

    如果我在 else 语句中添加此代码以将不是当前位置的单元格的颜色设置回默认值,它可以解决问题:[cell.leaveText setTextColor:[UIColor greyColor]];

    是的,没错。那是因为 UITableView 为了减少内存占用,重用了表格视图单元格。而且当一个单元格被重用时,UITableView 不会重置它的属性,所以必须将它们重置为默认值。您也可以在我的一些代码中看到这种模式,例如,this file browser UITableView 使用浅蓝色显示符号链接,但普通文件使用默认黑色。这里还可以看到条件语句的else分支。

    // test for symlink
    if (readlink([childPath UTF8String], NULL, 0) != -1) {
        cell.textLabel.textColor = [UIColor colorWithRed:0.1f green:0.3f blue:1.0f alpha:1.0f];
    } else {
        cell.textLabel.textColor = [UIColor blackColor];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-05
      • 2012-08-03
      • 1970-01-01
      • 2018-10-25
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 2014-09-04
      相关资源
      最近更新 更多