UIColor colorWithPatternImage只使用一张图片

5
我将尝试使用colorwithPatternImagetableviewcell设置不同的backgroundColor,但是效果与预期不符。文档中没有提到只能同时使用一种图案。例如,如果我有3行,我如下设置背景:
Cell1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"red.png"]];

Cell2.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"green.png"]];

Cell3.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"blue.png"]];

所有三行都将是红色。就好像存在某种全局颜色被返回。

colorWithPatternImage 对于每次调用都返回kCGColorSpaceModelPattern 1,无论传入的图片是什么。如果真的只有一个全局图案,则颜色应该是最后设置的那个,换句话说是蓝色。

这毫无意义。有没有人对苹果在这里做了什么有内部专业知识?

编辑 我甚至在完全不同的视图中使用了不同的图案,它仍然影响着其他视图的图案。我确信,虽然文档没有说明,但你一次只能使用一个UIColor图案。可悲。

2个回答

5
什么是Cell1?你是在哪个方法中进行设置的?
我认为你应该在某个方法中完成所有这些操作。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {      
      // create cell
    }

    // Set up the cell...

    // set up a background color
    if (something)
        cell.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"red.png"]];
    else (another)
        cell.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"green.png"]];
    else
        cell.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"blue.png"]];
}

我正在用那种方法来实现。因此,返回一个单元格以便在表格中显示。单元格[1-3] 实际上是开关语句中的每个 case 语句。 - Brenden

1
据我所见,这似乎不是或不再是真的。我这里有几个UITableViewCells,每个都有不同的背景图片,没有任何问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接