我有一个子类 UITableViewCell 并且在那个类中我有一个 drawRect 方法,它为每个单元格绘制一个框架。每个单元格都有不同的大小(如在 Twitter 应用程序中),由于某种原因,这导致我的滚动速度很慢。有没有解决的办法?
这是我在 UITableViewCell 子类中的 drawRect 代码:
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(c, [[UIColor whiteColor] CGColor]);
CGContextSetStrokeColorWithColor(c, [[UIColor lightGrayColor] CGColor]);
CGContextSetLineWidth(c, 0.5);
CGFloat minx = CGRectGetMinX(rect) , midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
CGFloat miny = CGRectGetMinY(rect) , midy = CGRectGetMidY(rect) , maxy = CGRectGetMaxY(rect) ;
minx = minx + 10;
miny = miny + 10;
maxx = maxx - 10;
maxy = maxy - 10;
CGContextSaveGState(c);
CGContextMoveToPoint(c, minx, midy);
CGContextAddArcToPoint(c, minx, miny, midx, miny, 4.0f);
CGContextAddArcToPoint(c, maxx, miny, maxx, midy, 4.0f);
CGContextAddArcToPoint(c, maxx, maxy, midx, maxy, 4.0f);
CGContextAddArcToPoint(c, minx, maxy, minx, midy, 4.0f);
// Close the path
CGContextClosePath(c);
// Fill & stroke the path
CGContextSetShadowWithColor(c, CGSizeMake(.5, 1), 2.5, [UIColor lightGrayColor].CGColor);
CGContextDrawPath(c, kCGPathFillStroke);
CGContextRestoreGState(c);
CGContextSetFillColorWithColor(c, [[UIColor whiteColor] CGColor]);
CGContextSetStrokeColorWithColor(c, [[UIColor lightGrayColor] CGColor]);
CGContextSetLineWidth(c, 0.5);
CGContextMoveToPoint(c, minx, midy);
CGContextAddArcToPoint(c, minx, miny, midx, miny, 4.0f);
CGContextAddArcToPoint(c, maxx, miny, maxx, midy, 4.0f);
CGContextAddArcToPoint(c, maxx, maxy, midx, maxy, 4.0f);
CGContextAddArcToPoint(c, minx, maxy, minx, midy, 4.0f);
CGContextClosePath(c);
CGContextDrawPath(c, kCGPathFillStroke);
}
这是单元格初始化和高度的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
TimeLineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TimeLineTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int cellHeight = 500 + indexPath.row;
return cellHeight;
}