我正在使用以下代码为 UITableViewCell 创建渐变背景。虽然这对普通表格单元格非常有效,但渐变仅出现在分组表格单元格的左右角。就好像然后应用了渐变,单元格被绘制在它上面。
有人可以建议对代码进行修改,使其适用于分组表格单元格吗?或者是否有完全不同的方法?
- (void)drawRect:(CGRect)rect {
CGContextRef c = UIGraphicsGetCurrentContext();
CGGradientRef myGradient;
CGColorSpaceRef myColorspace;
size_t num_locations = 2;
CGFloat locations[2] = {0.0, 1.0};
CGFloat components[8] = {0.8f, 0.8f, 0.8f, 1.0f, // Bottom Colour: Red, Green, Blue, Alpha.
0.9f, 0.9f, 0.9f, 1.0}; // Top Colour: Red, Green, Blue, Alpha.
myColorspace = CGColorSpaceCreateDeviceRGB();
myGradient = CGGradientCreateWithColorComponents (myColorspace, components,
locations, num_locations);
CGColorSpaceRelease(myColorspace);
CGPoint startPoint, endPoint;
startPoint.x = 0.0;
startPoint.y = self.frame.size.height;
endPoint.x = 0.0;
endPoint.y = 0.0;
CGContextDrawLinearGradient (c, myGradient, startPoint, endPoint, 0);
const CGFloat topSepColor[] = { 0.8f, 0.8f, 0.8f, 1.0f }; // Cell Seperator Colour - Top
CGGradientRelease(myGradient);
CGContextSetStrokeColor(c, topSepColor);
CGContextMoveToPoint(c, 0.0, 0.0);
CGContextSetLineWidth(c, 1.0);
CGContextSetLineCap(c, kCGLineCapRound);
CGContextAddLineToPoint(c, self.frame.size.width, 0.0);
CGContextStrokePath(c);
const CGFloat bottomSepColor[] = { 0.5f, 0.5f, 0.5f, 1.0f }; // Cell Seperator Colour - Bottom
CGContextSetStrokeColor(c, bottomSepColor);
CGContextMoveToPoint(c, 0.0, self.frame.size.height);
CGContextSetLineWidth(c, 1.0);
CGContextSetLineCap(c, kCGLineCapRound);
CGContextAddLineToPoint(c, self.frame.size.width, self.frame.size.height);
CGContextStrokePath(c);
[[UIColor blackColor] set];
}
感谢您的帮助。
最佳答案
我不明白为什么人们会尝试编写过于复杂的绘图例程来呈现自定义 UITableViewCell
。将单元格的 backgroundView
和 selectedBackgroundView
属性的值设置为标准的 UIImageView
,其中包含所有边框、斜面、渐变、圆角和任何其他你想要的就像在图片中一样。
对于圆形表格 View ,您将创建 4 张图像;一个用于顶部单元格,另一个用于底部单元格,一个用于中间单元格,如果您的表格只有一行,另一个用于单个单元格。
Matt Gallagher 在 Cocoa with Love 上写了一篇关于此的好文章,标题为 Easy custom UITableView drawing .
关于iphone - 将背景渐变应用于 Grouple 表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1979165/