如何使用Core图形

时间:2015-11-21 21:50:53

标签: ios objective-c uitableview core-graphics

我正在关注this tutorial核心图片。我正在改变UITableViewCell的背景颜色。我应该根据教程得到的结果是:

enter image description here

虽然我得到的结果是这个(完全红色的单元格是我在tableview中选择的单元格):

enter image description here

本教程适用于Xcode 4.6,这可能就是我没有得到理想结果的原因。这是我正在使用的代码。

自定义背景类:

CustomCellBackground.m

-(void) drawRect: (CGRect) rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    UIColor * redColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1];

    CGContextSetFillColorWithColor(context, redColor.CGColor);
    CGContextFillRect(context, self.bounds);
}

tableviewcontroller类中的tableview代码:

TableViewController.m


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * CellIdentifier = @"Cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSString * entry;

    if (![cell.backgroundView isKindOfClass:[CustomCellBackground class]]) {
        cell.backgroundView = [[CustomCellBackground alloc] init];
    }

    if (![cell.selectedBackgroundView isKindOfClass:[CustomCellBackground class]]) {
        cell.selectedBackgroundView = [[CustomCellBackground alloc] init];
    }


    if (indexPath.section == 0) {
        entry = self.thingsToLearn[indexPath.row];
    } else {
        entry = self.thingsLearned[indexPath.row];
    }
    cell.textLabel.text = entry;

    return cell;
}

请指出我做错了什么,并指出了正确的方向。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

您可以将标签颜色设置为清除颜色,如下所示。

SWIFT

cell.textLabel.backgroundColor = UIColor.clearColor()

目标C

cell.textLabel.backgroundColor = [UIColor clearColor];