我在 viewDidLoad
中有一个这样的数组,其中包含 UIColor
将用于填充表格 View 单元格背景颜色
self.ColorArray = [NSArray arrayWithObjects:
@"[UIColor redColor];",
@"[UIColor redColor];",
@"[UIColor redColor];", nil];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell.contentView.backgroundColor = [self.ColorArray objectAtIndex: indexPath.row % self.ColorArray.count];
}
但这会崩溃并抛出此错误:
-[__NSCFConstantString CGColor]: unrecognized selector sent to instance 0x137b28 -
最佳答案
您正在尝试将颜色作为字符串传递。
尝试传递为:
self.ColorArray = [NSArray arrayWithObjects:
[UIColor redColor],
[UIColor redColor],
[UIColor redColor], nil];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell.contentView.backgroundColor = [self.ColorArray objectAtIndex: indexPath.row % self.ColorArray.count];
}
关于ios - 如何用数组中的颜色填充表格 View 单元格背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32391553/