【问题标题】:Cycle list of colors and set them as cell background color | UITableView | Objective-C循环颜色列表并将它们设置为单元格背景颜色 | UITableView | Objective-C
【发布时间】:2016-04-01 19:42:14
【问题描述】:

假设我有一个十六进制颜色列表:{#5375ab, #D673DD, #fca565, #4f70a6, #f86cb5, #b9f3cd, #eff8a5, #f4adf9, #fe502e, #5375ab}

在我的 UITableView 中,有没有办法循环浏览颜色列表并将每个单元格设置为一种颜色?

例如背景颜色是这样的:

Cell 1 - backgroundColor: #5375ab
Cell 2 - backgroundColor: #D673DD
Cell 3 - backgroundColor: #fca565

...等等

【问题讨论】:

    标签: ios objective-c uitableview ios7


    【解决方案1】:

    是的,在indexPath.row 上使用% 模运算符。在您的 cellForRowAtIndexPath 实现中执行以下操作:

    NSArray *colors = @[yourColor1, yourColor2, yourColor3];
    int colorIndex = indexPath.row % colors.count;
    
    cell.backgroundColor = colors[colorIndex];
    

    【讨论】:

    • 您知道如何格式化颜色以使其被识别为十六进制颜色吗?我收到以下代码错误:NSArray *colors = @[@"#5375ab", @"#D673DD", @"#fca565", @"#4f70a6", @"#f86cb5", @"#b9f3cd", @"#eff8a5", @"#f4adf9", @"#fe502e", @"#5375ab", @"#ffffff"];
    • @jape 要么 stackoverflow.com/questions/1560081/… 要么只是查看 rgb 中的实际颜色并预先创建它们(如果它们不是动态的)
    • 将它们设置为[UIColor colorWithRed ...] 应该可以,对吧?另外,我需要和行一样多的颜色吗?它似乎不适合我
    • @jape yes colorWithRed 如果您提供 0.0 到 1.0 范围内的值,应该可以工作。不 - 你不需要像行一样多的颜色,这就是 % 的用途。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 2019-07-27
    • 2010-11-21
    • 1970-01-01
    • 2012-06-06
    • 2019-10-21
    相关资源
    最近更新 更多