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

那年仲夏 提交于 2019-12-14 01:10:46

问题


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.


回答1:


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.




回答2:


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




回答3:


You will either have to override the view or use a UIImageView. See UITableViewCell subview disappears when cell is selected for more information.



来源:https://stackoverflow.com/questions/3323644/how-to-subclass-uitableviewcell-and-use-it-to-not-clear-uilabel-background-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!