Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change each uitableviewcell background color

I want to change each cell background color in order.

This is my colors.And I just want to show them as shown in image.

I'm showing it with random colors now.But I want to show them in order.

var cellColors = ["F28044","F0A761","FEC362","F0BB4C","E3CB92","FEA375"]
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! GroupTableViewCell
        let randomColor = Int(arc4random_uniform(UInt32(self.cellColors.count)))
        cell.contentView.backgroundColor = UIColor(hexString: self.cellColors[randomColor])
    }

enter image description here

like image 826
Murat Kaya Avatar asked Aug 09 '16 08:08

Murat Kaya


People also ask

How to change UITableView background color?

Select the Tableview. Go to the Attributes Inspector. Set the Tableview Background Colour. Set the View Background Colour.

How do I remove background color in excel cell?

To remove any background colors, patterns, or fill effects from cells, just select the cells. Then click Home > arrow next to Fill Color, and then pick No Fill.

How do I add a background image to a uitableviewcell?

The UITableViewCell class allows developers to assign a custom background view using the “backgroundView:” method. To assign your own custom background image, we can create an UIImageView object with the background image and set it as the background view of the cell. Add the following code to the “cellForRowAtIndexPath:” method:

How do I add uilabel to a uitableviewcontroller prototype?

When you add a UITableViewController in the Story, by default, you should see an empty prototype cell. You can now add other UI control elements (e.g. UILabel, UIImageView) right into the prototype cell. Let’s first change the height of the cell. Select the Prototype Cells, click the “Size” inspector and change the row height from 44 to 71.

How to add background image to a table cell in Xcode?

In the Xcode project, you should find three background images for table cell. The images are specially designed for different types of cells. The UITableViewCell class allows developers to assign a custom background view using the “backgroundView:” method.

What is the default background color for the table view?

By default, the table view is displayed in white. We set its color to transparent so as not to block out the background of the parent view. Before you run the app, we’d like to make one more change.


1 Answers

You need to delete this line

let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! GroupTableViewCell

from your willDisplayCell function, because it already have your cell in parameters, and you just overriding it with new cell, and your new cell will be never used.

If you want to show colors in order, then you can use indexPath:

var cellColors = ["F28044","F0A761","FEC362","F0BB4C","E3CB92","FEA375"]
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.contentView.backgroundColor = UIColor(hexString: cellColors[indexPath.row % cellColors.count])
}
like image 71
Shadow Of Avatar answered Sep 23 '22 13:09

Shadow Of