[英]Can't draw in UITableViewCell's drawRect
我在使用自定義UITableViewCell的drawRect方法時遇到了麻煩。 這是我正在使用的代碼
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGPoint origin = _faceView.frame.origin;
CGFloat width = _faceView.frame.size.width;
CGFloat height = _faceView.frame.size.height;
CGFloat border = 2.0f;
CGPoint startPt = CGPointMake(origin.x + width/2, self.frame.size.height);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, startPt.x, startPt.y);
CGPoint basePt = CGPointMake(startPt.x, origin.y - height - border);
CGContextAddLineToPoint(ctx, basePt.x, basePt.y);
CGRect circleRect = CGRectMake(origin.x - border, origin.y - border, width + 2 * border, height + 2 * border);
CGContextAddEllipseInRect(ctx, circleRect);
UIColor *color = [UIColor redColor];
CGContextSetFillColorWithColor(ctx, color.CGColor);
CGContextSetStrokeColorWithColor(ctx, color.CGColor);
CGContextSetLineWidth(ctx, 1.0f);
CGContextDrawPath(ctx, kCGPathFillStroke);
}
我已經調試過以確保所有數值都有意義,看起來它們確實存在。 無法真正找出屏幕上沒有任何內容的原因。
值得一提的是,這也是一個在筆尖中定義的單元格。 我正在使用iOS 7 sdk構建。
有任何想法嗎?
tahnks
嘗試將backgroungcolor屬性設置為透明色
self.backgroundColor = [UIColor clearColor]
您可能不應該在UITableViewCell自己的drawRect中執行此操作。 而是創建一個自定義UIView並將其添加為子視圖。
另見這個答案 。
你必須設置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
.....
[cell setNeedsDisplay];
return cell;
}
嘗試設置contentView
屬性的backgroundColor
以清除顏色
self.contentView = [UIColor clearColor];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.