Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarify undocumented behavior of UITableViewCell on highlight event

UITableViewCell modifies the contents of it's contentView hierarchy when the cell is is highlighted (user touches down on the cell).

Two examples I've found to date:

  • Put a UIView in a cell's contentView with a background color. That UIView's background color is removed when the table cell is highlighted (it's apparently set to have a clear background.)
  • Put a UIButton in contentView. When the cell is highlighted, the button is also forced into the highlighted state.

It's as if there's some logic in the UITableView cell that inspects all views in the cell's hierarchy and modifies them according to a set of mysterious rules, then restores them back to normal once the cell is un-highlighted.

Can anyone explain what and why UITableViewCell is modifying (unexpectedly and in an undocumented fashion) in the contents of my custom table view cells?

Thanks!

like image 509
Yetanotherjosh Avatar asked Nov 14 '22 00:11

Yetanotherjosh


1 Answers

The highlighting of the UITableViewCell is documented in the Apple docs as follows:

The highlighting affects the appearance of labels, image, and background. When the the highlighted state of a cell is set to YES, labels are drawn in their highlighted text color (default is white). The default value is is NO. If you set the highlighted state to YES through this property, the transition to the new state appearance is not animated. For animated highlighted-state transitions, see the setHighlighted:animated: method.

Note that for highlighting to work properly, you must fetch the cell’s labels using the textLabel and detailTextLabel properties and set each label’s highlightedTextColor property; for images, get the cell’s image using the imageView property and set the UIImageView object’s highlightedImage property.

This does not mention UIButtons, but this post is about how to prevent the button from going into the highlighted state when the cell does.

like image 152
albertamg Avatar answered Dec 21 '22 07:12

albertamg