正如我在问题here中讨论的那样,我尝试使用CAGradientLayer设置UITableView的背景色。我正在像这样在NSMutableArray中填充一些UITableView值...

- (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;
}

发生的情况是,填充有初始值的单元格似乎与我设置的渐变颜色重叠。在这些单元格中没有文本/边框出现(在这种情况下,单元格的值为1234)。我在这儿做错了什么?

最佳答案

将backgroundView设置为tableView对我有用。这是我的操作方式:

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 - iOS UITableView:使用CAGradientLayer会使初始对象不出现在表格 View 中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10225935/

10-17 01:10