How to change background color of TableView items on iOS?

iOSApps/ApplicationsMobile Development

Changing the background color of table view items is different from changing the background color of the table view. New programmers may often confuse between these two things, In this post, we will be seeing how to change the background color of TableView items i.e. cells.

So let’s get started.

For changing the background color of the table view cell, you should change the contentView.backgroundColor property of the cell.

Add the below code in your cellForRowAt indexPath method,
cell.contentView.backgroundColor = UIColor.cyan

Your method should look like something below,

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
   cell.contentView.backgroundColor = UIColor.cyan
   return cell
}

Now run the project to see the effect.

raja
Published on 30-Aug-2019 10:43:34
Advertisements