2016-05-04 93 views
2

任何人都可以看到为什么我不能将我的tableView单元格的背景颜色属性设置为我在ViewDidLoad()中创建的数组的颜色?我的viewDidAppear()方法中也有一个self.tableView.reloadData()。让我知道。为什么我无法从颜色数组中随机设置颜色到tableViewCells?

override func viewDidLoad() { 
    super.viewDidLoad() 

    colorArray += [UIColor.whiteColor(), UIColor.blackColor(), UIColor.greenColor(), UIColor.yellowColor(), UIColor.redColor(), UIColor.purpleColor(), UIColor.orangeColor(), UIColor.blueColor(), UIColor.brownColor(), UIColor.darkGrayColor(), UIColor.lightGrayColor(), UIColor.grayColor(), UIColor.cyanColor(), UIColor.magentaColor(), UIColor.clearColor()] 

} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 

    for color in colorArray { 
     cell.backgroundColor = color 
    } 

    return cell 
} 
+0

哪里是在你的代码的随机部分? – Wain

+0

http://stackoverflow.com/questions/29779128/how-to-make-a-random-background-color-with-swift也许这对你有帮助 – Patrick

+0

@jamie Baker看到我的答案 –

回答

0

设置单元的随机背景。工作代码。这里arc4random_uniform()取数字0到colorArray.count。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 
    cell.backgroundColor = colorArray[arc4random_uniform(colorArray.count)] 
    return cell 
} 
0

这将设置每次最后的UIColor循环,

for color in colorArray { 
    cell.backgroundColor = color 
} 

所以,你只需要通过indexPath.row(或部分)得到它:

cell.backgroundColor = colorArray[indexPath.row] 

或者得到它随机是这样的:

let randomInt = Int(rand()) % array.count 
cell.backgroundColor = colorArray[randomInt] 
1

你重复dly设置单元格的颜色。

每次表格视图想要加载新单元格时,都会调用其数据源'tableView(:cellForViewAtIndexPath:)方法。

在实现此方法时,您将遍历数组并将单元格的颜色设置为数组中的每个项目。所以首先它将是白色,然后是黑色,然后是绿色......最后它被设置为clearColor,这是透明的。

每次该方法被调用时,您的单元格的颜色都将被设置为透明。好难过!

通过看你的代码,我想你想要的是设置第一个单元格的颜色为白色,第二个黑色的,第三个为绿色,等等

你应该知道,每次tableView(:cellForRowAtIndexPath:)被称为,它为您提供一个参数,告诉您它想要哪个单元。你只需要使用的,要实现你的方法:

// replace your for loop with this 
cell.backgroundColor = colorArray[indexPath.row] 

这样一来,当它想第一个单元格,则单元格设置为第一种颜色。当它想要第二个单元格时,将单元格设置为第二个颜色等。

另一方面,如果要从该colorArray中随机选取,以便每次加载表视图时单元格都会不同,只需要产生随机数,并调用的colorArray下标与该号码:

// replace your for loop with this 
let randomNumber = arc4random_uniform(UInt32(colorArray.count)) // line 1 
cell.backgroundColor = colorArray[randomNumber] // line 2 

线1生成0和colorArray.count之间的随机数。而第2行只是将单元格的颜色设置为colorArray索引处的颜色!

简单明了吧?

编辑:更多关于arc4random_uniform

您一定想知道这是什么arc4random_uniform功能在这里做。

arc4random_uniform返回0(含)和自变量(不含)之间的随机数(均匀分布)。换句话说,arc4random_uniform(5)将返回0,1,2,3,4而不是5.

该函数采用UInt32类型的参数,因此我无法直接传入colorArray.count

0

试试这个代码,

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 

     cell.backgroundColor = self.colors[indexPath.row % self.colors.count] 

     return cell 
    } 

希望它有帮助

0

这将无法工作,因为的tableView(的cellForRowAtIndexPath)为每个单元格时调用一次。

现在发生的事情是,您的cell.backgroundColor被设置为第一种颜色,然后紧接着第二种颜色等。因此,backgroundColor始终是colorArray中最后一个元素的颜色。

使用这个,而不是你的for-in循环:

cell.backgroundColor = colorArray[indexPath.row] 

如果你在你的tableView更多的细胞比你的数组中的颜色使用: (这将在0重新开始当过colorArray.count

override func viewDidLoad() { 
    super.viewDidLoad() 

    colorArray += [UIColor.whiteColor(), UIColor.blackColor(), UIColor.greenColor(), UIColor.yellowColor(), UIColor.redColor(), UIColor.purpleColor(), UIColor.orangeColor(), UIColor.blueColor(), UIColor.brownColor(), UIColor.darkGrayColor(), UIColor.lightGrayColor(), UIColor.grayColor(), UIColor.cyanColor(), UIColor.magentaColor(), UIColor.clearColor()] 

} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 

    let cycle = i/colorArray.count 
    let colorIndex = i - (cycle * colorArray.count) 

    cell.backgroundColor = colorArray[colorIndex] 

    return cell 
}