Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subClass UITableViewCell and use it to not clear UILabel background color on UITabeViewCell selected?

In my app, I use a label to display a specified color by set background color in a customized UITableViewCell (because this color maybe changed according incoming data from internet), after viewDidLoad, everything is ok, but when this cell is selected (highlighted) the color is cleared.

After searching, I found out that some someone have to subclass UITableViewCell and overwrite setHighlight method to not clear label background color. I have tried but unlucky.

So Does anybody know how to do this? the right way to subclass UITableViewCell, then use it in UITableViewController to not clear label background color? Please help me.

Thanks for any advice.

like image 576
Son Nguyen Avatar asked Jan 22 '23 17:01

Son Nguyen


2 Answers

From the latest UITableViewCell documentation:

Note: If you want to change the background color of a cell (by setting the background color of a cell via the backgroundColor property declared by UIView) you must do it in the tableView:willDisplayCell:forRowAtIndexPath: method of the delegate and not in tableView:cellForRowAtIndexPath: of the data source. Changes to the background colors of cells in a group-style table view has an effect in iOS 3.0 that is different than previous versions of the operating system. It now affects the area inside the rounded rectangle instead of the area outside of it.

Also, review the Table View Programming Guide for iOS for some of the best documentation.

like image 110
petert Avatar answered Jan 29 '23 12:01

petert


override - (void)layoutSubviews of cell and set label.backgroundColor in there. that's work for me

like image 39
Nick Avatar answered Jan 29 '23 10:01

Nick