【发布时间】:2010-10-27 15:37:36
【问题描述】:
我正在使用 answer 中的 CAGradientLayer 方法在我的 UITableViewCells 上设置渐变。
但是,对于这个表格,我通过 tableView:heightForRowAtIndexPath: 增加了单元格的高度。我的渐变层现在没有覆盖单元格的整个高度,而是停在原始高度(参见pic)。
我尝试将图层的框架设置为 tableView:cellForRowAtIndexPath: 中的单元格边界:但这也不起作用。有什么方法可以指定图层应该自动调整大小吗?
谢谢!
代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 55;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.colors = [NSArray arrayWithObjects:
(id)[[UIColor purpleColor] CGColor],
(id)[[UIColor redColor] CGColor],
nil];
[cell.layer insertSublayer:gradient atIndex:0];
}
cell.layer.borderWidth = 1.0;
CALayer* gradientLayer = [cell.layer.sublayers objectAtIndex:0];
DebugLog(@"cell bounds: %@", NSStringFromCGRect(cell.bounds));
gradientLayer.frame = cell.bounds;
// Configure the cell...
return cell;
}
【问题讨论】:
-
请从 cellForRowAtIndexPath 和 heightForRowAtIndexPath 发布您的代码。
标签: iphone objective-c uitableview gradient