Ios UITableView中的图形文字-文字不显示

Ios UITableView中的图形文字-文字不显示,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我是iOS新手,所以可能很简单。重要事项-我想使用绘图,而不是添加子视图。我需要用公共方法来做。试着这样做: @implementation TripTableViewCell2 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdenti

我是iOS新手,所以可能很简单。重要事项-我想使用绘图,而不是添加子视图。我需要用公共方法来做。试着这样做:

@implementation TripTableViewCell2

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)updateWithTrip:(Trip*)trip
{
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:10.0f],
                                 NSForegroundColorAttributeName: UIColorFromRGB(0x28cdfb)};
    CGSize textSize = [trip.tripId sizeWithAttributes:attributes];
    CGPoint textPoint = CGPointMake(10.0f, 10.0f);
    [trip.tripId drawAtPoint:textPoint withAttributes:attributes];
}

我想我错过了一些简单的事情,比如为绘图设置上下文,但我不确定。。另外,是否有任何一行命令可以擦除此视图中绘制的任何内容?

NSString drawAtPoint:withFont:
使用上下文堆栈,从我调用此方法的位置,堆栈为空。用包装纸包装电话

UIGraphicsPushContext(context);和UIGraphicsPopContext()

成功了。

问题在于,当您调用drawAtPoint时,它会将当前上下文(而不是单元格的上下文)引入

您需要做的是创建UIView的子类,并使用drawRect方法进行绘图。在这里,我创建了这样一个类,并对其进行了测试,使其能够按预期工作:

#import "CustomDraw.h"

@implementation CustomDraw

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    NSString *trip = @"My trip";
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:20.0f],
                                 NSForegroundColorAttributeName: [UIColor redColor]};
    CGSize textSize = [trip sizeWithAttributes:attributes];
    CGPoint textPoint = CGPointMake(10.0f, 10.0f);
    [trip drawAtPoint:textPoint withAttributes:attributes];
}


@end

在表格单元格中显示视图的一种方法是将其设置为背景视图:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TripCell"];
        cell.backgroundView = [[CustomDraw alloc] init]; // Pass your trip in here

    return cell;
}