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!
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.
Related
I m having issue of table cells reordering. I have some views with some background color in cell contenview.
When we drag cell for reorder its making all view's color to clear color.
its a standard behavior as much i understand.
Is there any way to not affect subview color while reordering?
The default behavior is to allow the user to see though the cell to best determine where to drop it, so strongly consider that use case before implementing something else.
You could try overriding setHighlighted: or setSelected: on the cell (or any applicable
subviews like UILabels) to ignore any changes when the controller is reordering.
I am using SSToolkit and SSCollectionView. When I click a SSCollectionViewItem, I would like to darken the item so the user knows it has been pressed.
The documentation (http://sstoolk.it/documentation/Classes/SSCollectionViewItem.html) shows a setSelected and setHighlighted method, but I am not sure how then to change the appearance of my item.
Any help?
The answer is in the documentation:
Discussion
Highlights or unhighlights the item,
animating the transition between
regular and highlighted state if
animated is YES. Highlighting affects
the appearance of the items's labels,
image, and background.
Note that for highlighting to work properly, you must fetch the item's
label (or labels) using the textLabel
(and detailTextLabel) properties and
set the label's highlightedTextColor
property; for images, get the items's
image using the imageView property and
set the UIImageView object's
highlightedImage property.
A custom table item may override this
method to make any transitory
appearance changes.
SSCollectionViewItem
I have a custom subclass of UITableViewController that uses a set of custom subclass of UITableViewCells.
There is nothing too fancy. The table cell's have two subviews in their content view.
Right now I have the user select a cell, a navigation controller pushes to a color selection screen. The user selects a color. Great. The table view is a delegate of this view. It receives the color, updates the selected cell so that the subview now uses this background color, and then pops the current view from the navigation controller.
It all works! The tableViewController slides back into view and the indicator in the cell now displays the background color the user selected. BUT after the table view appears, the cell selected cell gets a blue highlight applied to it automatically. This is some built in magic from the TableViewController. It's nice but it's resetting my cell's indicator status.
Problem is, I can't find out where to properly update the cell to this new color. The color is stored as an ivar in the tableViewController. So I thought trying this out at:
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
But alas this method doesn't fire as a result of the magic UIKit is doing to briefly highlight the cell. Any ideas where I need to update my cell properly?
I'm not sure how you are setting the background color yet, but you should be using the backgroundView property of your UITableViewCell subclass.
someCell.backgroundView.backgroundColor = someColor;
(from inside your UITableViewController, or wherever you are getting that color information back from your picker).
Doing this should not reset the background color when the cell goes from selected to deselected.
I am display some long text in a cell, and resizing the height of the cell using heightForRowAtIndexPath. However, when the text is displayed it is running into the area used by the (blank) disclosure indicator.
When such a row is selected, and the checkmark is displayed, the text reformats itself to not use the indicator area, causing a visual effect I do not want.
If there was a UITableViewCellAccessoryBlank accessory type (rather than UITableViewCellAccessoryNone), maybe the text wouldn't wrap into that area when displaying. Am I going to have to create a custom cell and layout my own label, or is there a simpler way?
First of all, I don't see a property call UITableViewCellAccessoryBlank in the UITableView Cell class reference so I don't think this will work.
I think you have 2 options :
Create a custom cell, like you suggest.
Configure the textLabel of your cell to change his contentMode.
I read this in UILabel class reference :
The default content mode of the
UILabel class is
UIViewContentModeRedraw. This mode
causes the view to redraw its contents
every time its bounding rectangle
changes. You can change this mode by
modifying the inherited contentMode
property of the class.
I suppose that the textLabel bounds change every time you change the accessory type, so by default it redraw himself.
You can try this in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method :
cell.textLabel.contentMode = UiViewSomeContentMode;
Content mode list can be found here. I'm not sure which one you should use so I let you try.
EDIT
It seems that contentMode is not working. So you should use a custom UITableViewCell to prevent any animation when adding an accessoryView.
Hope this helps !
My UITableViewController uses a custom UITableViewCell Subclass.
The subClass (QuoteCell - loaded from NIB) has a few UILabels and a UIImageView on it.
Works fine, however, when I tap on the cell, the cell highlights, but the UILabels on the cell don't reverse colors.
I thought this was stock behavior??
Any help appreciated, Thanks!
btw: There's nothing in the didSelectRowAtIndexPath method yet.
I found the answer, after reading comments from Jasarien and Prakash.
Apparently, in IB, you manually have to select a highlight color to see the behavior.
By default, UILabel color is Black and the highlight color apparently is also Black, which is strange because it has that half black, half white diagonal. I would think the behavior would be different.
Anyway, I changed the color to solid white and got the highlighted behavior I was expecting.
I wonder why you need to show the row selection?
You could do this
cell.selectionStyle = UITableViewCellSelectionStyleNone;
and handle your row selection logic as-is..
You have to write the code to swap the text colour of the label yourself.
The best place for that would probably be in -setHighlighted:animated: