我正在研究
UITableView
其单元格包含
UIImageView
从URL获取数据并将图像缓存到iPhone磁盘的子类。
问题是,对于缓存图像的事件,滚动往往会结巴。所以我搜索了一下,发现
ABTableViewCell
(github.com/enormego/abtableviewscell),应该可以显著提高滚动的平滑度。
但是,即使有了所提供的示例(blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview),我也不知道该怎么做。
我试图这样做:我创建了一个继承
ababelVIEW单元格
,添加了一些uiLabels和uiImageView作为类属性,并以这种方式实现了方法:在
initialize
类方法,将它们存储在静态指针中,然后在中设置类属性
- (void)drawContentView:(CGRect)r highlighted:(BOOL)highlighted
以及示例中显示的背景颜色设置。结果如下:
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);
}
这给了我完全黑色的单元格,有时一个有正确颜色的文本和图像集,但它的内容随着我向下滚动而改变。
很明显我不明白我该怎么做
drawContentView
.
有人能解释一下它的目的吗?