UIColor colorWithPatternImage仅使用一个图像
-
05-07-2019 - |
题
我正在尝试使用 colorwithPatternImage
为 tableviewcell
提供不同的 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"]];
所有3行都是红色的。就好像有一些全局颜色被返回。
无论传入什么图像, colorWithPatternImage
都会为每次调用返回 kCGColorSpaceModelPattern 1
。如果确实一次只有1个全局模式,那么颜色应该是最后一组,换句话说是蓝色。
这没有任何意义。有没有人对Apple在这里做什么有内部专业知识?
修改强> 我甚至在完全独立的视图中使用不同的模式,它仍然影响其他视图的模式。我确信,虽然文档没有说明这一点,但你一次只能使用一个UIColor图像模式。悲伤。
解决方案
到目前为止,我可以看到这不是或不再是真的。我这里有几个UITableViewCells,每个都有不同的backgroundImage,没有任何问题。
其他提示
什么是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"]];
}
不隶属于 StackOverflow