综合技术

IOS UITableView: usingCAGradientLayer causes initial objects to appear in the table view

As I discussed in the question here, I tried to set the background color of a UITableView using CAGradientLayer. I am populating the UITableView with some values in NSMutableArray like this…

- (void)viewDidLoad
{
    // ....
    if (!navigationItems) {
        navigationItems = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", nil];
    }

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:213.0f/255.0f green:91.0f/255.0f blue:92.0f/255.0f alpha:1.0f] CGColor][[UIColor colorWithRed:213.0f/255.0f green:0.0f blue:0.0f alpha:1.0f] CGColor], nil];
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = colors;
    gradientLayer.frame = tableView.bounds;

    [tableView.layer insertSublayer:gradientLayer atIndex:0];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.textLabel.text = [navigationItems objectAtIndex:indexPath.row];
    cell.textLabel.textColor = [UIColor blackColor];

    return cell;
}

What happens is that the cells being populated with the initial values appear to have been overlapped with the gradient color I set. No text/border appears in those cells (cells with values 1
, 2
, 3
, 4
in this case). What am I doing wrong over here?

Setting a backgroundView to the tableView worked for me. Here’s how I did it:

CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = colors;
gradientLayer.frame = tableView.bounds;

//[tableView.layer insertSublayer:gradientLayer atIndex:0];

UIView *tableBackgroundView = [[UIView alloc] init];
[tableBackgroundView.layer insertSublayer:gradientLayer atIndex:0];
[self.tableView setBackgroundView:tableBackgroundView];
展开阅读全文

微信扫一扫,分享到朋友圈

IOS UITableView: usingCAGradientLayer causes initial objects to appear in the table view
0

智能绿色引领未来交通交通科技博览会体现“大交通”格局

上一篇

华为对外媒证实鸿蒙OS存在:正在国内测试、最快数月内就绪

下一篇

你也可能喜欢

评论已经被关闭。

插入图片
IOS UITableView: usingCAGradientLayer causes initial objects to appear in the table view

长按储存图像,分享给朋友