I want to change the backgroundColor
of the UITableViewCell
and thus want to get the index number of each table view cell, because I also want to change the color of those cells with respect to its index number.
However how can I get the index number from within tableView: cellForRowAtIndexPath:
? For example, in the following code (within tableView: cellForRowAtIndexPath:
):
let numberOfCell = indexPath.length // always returns 2, even if I add more cells
println(indexPath) // returns <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}
for i in 0..<numberOfCell {
let alpha = CGFloat(1.0 - Double(i) / 10.0)
cell.backgroundColor = UIColor(red: 227/256, green: 245/256, blue: 61/256, alpha: alpha)
}
return cell
I want to change the alpha
value of each cell with respect to the index number, but the code above makes all the cell's alpha
to 1.0
, because the numberOfCell
is always 2
, even though my table view has four objects. I'm not sure why it always returns 2
. I added more cells but the result didn't change.
So why does it return 2
and how can I get the correct value of each index? What I want to get is 0
for the first cell, 1
for the second, 2
for the third, and goes on.
I also tried to access the path
property of the index path as shown on the output of println(indexPath)
above, but for some reasons NSIndexPath
doesn't have such properties...
Try indexPath.row
or, if you are using sections too, try indexPath.section
in addition. :)