How to customize the background color of a UITableViewCell?
You need to set the backgroundColor of the cell's contentView to your color. If you use accessories (such as disclosure arrows, etc), they'll show up as white, so you may need to roll custom versions of those.
How to change each uitableviewcell background color
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])
}
Custom UITableViewCell background color
Color in iOS ranges between 0-1
. You need to add 255 as factor to get correct color code, see below
cell.backgroundColor = UIColor(red: 43/255.0, green: 65/255.0, blue: 87/255.0, alpha: 1);
It should work.
Cheers.
Change UITableViewCell background color in SwiftUI
Change background color of a view by simply adding .background property
var coinList : some View {
List {
ForEach(0.. CoinEntry(coin: coins[i]).background(MAIN_DARK_GRAY)
}.onDelete(perform: { indexSet in
self.coins.remove(atOffsets: indexSet)
})
}.frame(width: UIScreen.main.bounds.width).background(MAIN_DARK_GRAY)
}
Or you can use listRowBackground property
var coinList : some View {
List {
ForEach(0.. CoinEntry(coin: coins[i])
}.onDelete(perform: { indexSet in
self.coins.remove(atOffsets: indexSet)
})
}.frame(width: UIScreen.main.bounds.width).listRowBackground(MAIN_DARK_GRAY)
}
Change background color of selected UITableViewCell
Add this code in cellForRowAtIndexPath
. Thanks to @danh, added check if there's already selectedBackroundView
so no need to allocate new one.
if cell.selectedBackgroundView == nil{
cell.selectedBackroundView = UIView()
}
cell.selectedBackgroundView.backgroundColor = // your color
UITableView set background color
If you want the cell background color to continue to alternate, then you need to lie about how many rows are in the table. Specifically, in tableView:numberOfRowsInSection you need to always return a number that will fill the screen, and in tableView:cellForRowAtIndexPath, return a blank cell for rows that are beyond the end of the table. The following code demonstrates how to do this, assuming that self.dataArray is an NSArray of NSStrings.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ( self.dataArray.count < 10 )
return( 10 );
else
return( self.dataArray.count );
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleCell"];
if ( indexPath.row % 2 == 0 )
cell.backgroundColor = [UIColor orangeColor];
else
cell.backgroundColor = [UIColor redColor];
if ( indexPath.row < self.dataArray.count )
cell.textLabel.text = self.dataArray[indexPath.row];
else
cell.textLabel.text = nil;
return cell;
}
How do I set the background color of the editing accessory of a table view?
You have to do it in the tableView:willDisplayCell:forRowAtIndexPath:
method. You can it like this:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "MyText"
return cell
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.backgroundColor = UIColor(red: 0xca / 0xff, green: 1, blue: 0xc7 / 0xff, alpha: 1)
}
Edit:
Here is the visual result of the code above:
Change Background Color of UITableViewCells when Switch is on - Swift
For the background color of the UITableViewCells, you could have a boolean that indicates if the night mode is on, as you have now.
When there is a change in the switch value (from off to on or viceversa), you should call tableView.reloadData()
. This way, the method cellForIndexPath
will be called again for each one of the cell. In this method, you should check if the night mode is on (with the boolean) and therefore set the backgroundColor of the cell accordingly.
For the navigationBar, you could use the property called barTintColor. You can use it the following way
UINavigationController?.navigationBar.barTintColor = //your color
Also, remember that you should implement the datasource methods of the tableView. Since your tableviewController is already the datasource
and delegate
, you just have to override them. There are 3 important ones.
//Number of sections
override func numberOfSections(in tableView: UITableView) -> Int
//Number of rows in a section
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
//In this one you setup or return a tableviewcell for the specific
//IndexPath. Here you should create a UITableViewCell and set its background
//color accordingly to the value of the switch
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
This methods are basic for the UITableViewController. This may be out of scope for the question, you could check out more in several sources out there. This is an example that explains a little bit more https://www.ralfebert.de/tutorials/ios-swift-uitableviewcontroller/
Related Topics
How to Pass Data Between View Controllers in Swift
Detecting the Call Events in Ios
Detecting Sheet Was Dismissed on iOS 13
How to Upload Images to a Server in iOS With Swift
Resize Uiimage With Aspect Ratio
How to Take a Full Screen Screenshot in Swift
Difference Between Viewdidload and Viewdidappear
Correct Singleton Pattern Objective C (Ios)
Change Uitableview Height Dynamically
Sorting Array Alphabetically With Number
#Warning: C-Style For Statement Is Deprecated and Will Be Removed in a Future Version of Swift
Iterate Over Snapshot Children in Firebase
How to Animate the Background Color of a Uilabel
How to Add Objects to a Uiscrollview That Extend Beyond Uiview from Storyboard