自定义UITableViewCell具有不同的行高和drawRect

时间:2012-11-07 02:57:38

标签: dynamic uitableview height row drawrect

我有一个子类UITableViewCell,在该类中我有一个drawRect方法,为每个单元格绘制一个框架。每个单元格都有不同的大小(如在Twitter App中),由于某种原因,这导致我的滚动速度变慢。有办法解决这个问题吗?

这是我在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;
}

0 个答案:

没有答案