gpt4 book ai didi

ios - 如果数组在末尾,如何遍历数组并重新开始?

转载 作者:行者123 更新时间:2023-11-28 09:53:58 25 4
gpt4 key购买 nike

我有一组颜色,我想将其应用于 iOS 中的 uitableviewcells

let colorPalet = [
UIColor(red: 255.0/255.0, green: 159.0/255.0, blue: 112.0/255.0, alpha: 1),
UIColor(red: 81.0/255.0, green: 218.0/255.0, blue: 168.0/255.0, alpha: 1),
UIColor(red: 2.0/255.0, green: 207.0/255.0, blue: 255.0/255.0, alpha: 1),
UIColor(red: 144.0/255.0, green: 153.0/255.0, blue: 166.0/255.0, alpha: 1)
]
cell.backgroundColor = colorPalet[indexPath.row]

问题是,当 indexPath.row 大于 colorPalet 数组时,它将崩溃,因为数组中没有更多条目。如果在 Swift 中数组位于数组的末尾,如何再次开始遍历数组?

最佳答案

你可以使用模数:

cell.backgroudColor = colorPalet[indexPath.row % colorPalet.count]

关于ios - 如果数组在末尾,如何遍历数组并重新开始?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40322839/

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