gpt4 book ai didi

ios - 如何用数组中的颜色填充表格 View 单元格背景?

转载 作者:行者123 更新时间:2023-11-29 12:14:07 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com