开发者

iPhone - How to use ABTableViewCell?

I'm working on a UITableView whose cells contain an UIImageView subclass which gets data from a URL and cache images to the iphone disk.

Problem is, event with cached images the scrolling tends to be stuttering. So I searched a bit and found ABTableViewCell ( github.com/enormego/ABTableViewCell ) which is 开发者_如何学Gosupposed to dramatically improve scrolling smoothness.

But, even with the example provided ( blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview ) I don't really get what I am supposed to do.

I tried to do this: I created a class which inherits ABTableViewCell, added some UILabels and the UIImageView as class properties, and implemented methods this way: allocate and initialize subviews (labels, image) in the initialize class method, storing them in static pointers, and then set class properties in - (void)drawContentView:(CGRect)r highlighted:(BOOL)highlighted along with background color setting shown in example. Here's the result:

static AsyncUIImageView* image = nil; // A subclass using ASIHTTPRequest for image loading
static UILabel*       label1 = nil;
static UILabel*       label2 = nil;

+ (void)initialize {
    if (self == [ResultTableViewCell class]) {
        image = [[[AsyncUIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 60)] retain];

        label1 = [[[UILabel alloc] initWithFrame:CGRectMake(90, 5, 150, 30)] retain];
        label1.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
        label1.textColor = [UIColor purpleColor];
        label1.backgroundColor = [UIColor clearColor];

        label2 = [[[UILabel alloc] initWithFrame:CGRectMake(180, 8, 100, 25)] retain];
        label2.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
        label2.textColor = [UIColor grayColor];
        label2.backgroundColor = [UIColor clearColor];
    }
}

- (void)drawContentView:(CGRect)r highlighted:(BOOL)highlighted {
    if (self.imageView == nil) {
        self.imageView = image;
        [self addSubview:image];

        self.firstLabel = label1;
        [self addSubview:label1];

        self.secondLabel = label2;
        [self addSubview:label2];
    }

CGContextRef context = UIGraphicsGetCurrentContext();

    UIColor *backgroundColor = [UIColor whiteColor];
    UIColor *textColor = [UIColor blackColor];

    if (self.selected || self.highlighted) {
        backgroundColor = [UIColor clearColor];
        textColor = [UIColor whiteColor];
    }

    [backgroundColor set];
    [textColor set];
    CGContextFillRect(context, r);      
}

This gives me completely black cells, sometimes one has text and image set with correct colors, but its content changes as I scroll down. Obviously I did not understand what I am supposed to do in drawContentView.

Could someone explain its purpose?


The whole idea is to not add subviews, but to draw the text instead. Eg.

- (void)drawContentView:(CGRect)r highlighted:(BOOL)highlighted {

    [someText drawInRect:r withFont:aFont];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜