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

标签 ios uitableview

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

相关文章:

ios - 选择单元格时 TableViewCell 内容重复

iphone - 具有单个圆角的 UIButton

iphone - 为 UITableView 添加阴影

ios - 在 Web 服务异步回调上更新 UITableView

ios - GDB 转储内存 .bin 文件为空

iphone - XCode 引用类型 - 相对于项目

objective-c - UITableViewRowAnimation 自定义动画

ios - JSON 未在 TableView 中加载

ios - UISearchBar 取消按钮不起作用

ios - 关闭 UIImagePickerController 后屏幕尺寸错误