I want to display data on table view cell from the database so I want to give the color to cell according to data means for 1st cell gray then next two cells in brown again I want display gray cell, so can anyone tell me what is the simplest procedure for it?
Basic Swift Code for iOS Apps For changing the background color of the table view cell, you should change the contentView. backgroundColor property of the cell. Now run the project to see the effect.
A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 2 == 0) {
[cell setBackgroundColor:[UIColor whiteColor]];
}
else {
[cell setBackgroundColor:[UIColor lightGrayColor]];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With