我正在开发一个 UITableView
,其单元格包含一个 UIImageView
子类,该子类从 URL 获取数据并将图像缓存到 iPhone 磁盘。
问题是,如果使用缓存图像,滚动往往会出现卡顿。所以我搜索了一下,发现 ABTableViewCell
( github.com/enormego/ABTableViewCell ) 应该可以显着提高滚动平滑度。
但是,即使提供了示例(blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview),我仍然不明白我应该做什么。
我尝试这样做:我创建了一个继承 ABTableViewCell
的类,添加了一些 UILabels 和 UIImageView 作为类属性,并以这种方式实现方法:分配和初始化 subview (标签,图像) initialize
类方法,将它们存储在静态指针中,然后在 - (void)drawContentView:(CGRect)rhighlighted:(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
中做什么。
有人可以解释一下它的用途吗?
最佳答案
整个想法是不添加 subview ,而是绘制文本。 例如。
- (void)drawContentView:(CGRect)r highlighted:(BOOL)highlighted {
[someText drawInRect:r withFont:aFont];
}
关于iPhone - 如何使用 TableView 单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3085980/