Today, I encountered a situation where when I click UITableViewCell to highlight, the highlighted objects in its subviews are also highlighted. After a long time, I did not solve the problem. I did not find a way to study it with my colleagues, later, I found a solution on the Internet. Now let's share it!
To customize the UI, The accessoryView of UITableViewCell is modified as follows:
UIButton * accessoryDetailDisclosureButton = [[UIButton alloc] initWithFrame: CGRectMake (0, 0, 30, 30)];
[AccessoryDetailDisclosureButton setImage: [UIImage imageNamed: @ "accessoryDetailDisclosureButton_normal.png"] forState: UIControlStateNormal];
[AccessoryDetailDisclosureButton setImage: [UIImage imageNamed: @ "accessoryDetailDisclosureButton_highlighted.png"] forState: UIControlStateHighlighted];
[AccessoryDetailDisclosureButton addTarget: self action: @ selector (accessoryDetailDisclosureButtonPress :) forControlEvents: UIControlEventTouchUpInside];
Cell. accessoryView = accessoryDetailDisclosureButton;
[AccessoryDetailDisclosureButton release];
Achieve the following results
However, when the cell is selected, the accessoryView state is also changed to highlighted, as shown below:
The final solution is to inherit the UITableViewCell to modify the status when the cell is pressed, as shown below:
@ InterfaceUCaiTableViewCell: UITableViewCell
@ End
@ ImplementationUCaiTableViewCell
-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {
[Super touchesBegan: touches withEvent: event];
[(UIButton *) self. accessoryView setHighlighted: NO];
}
@ End
The following results are achieved:
The above modification method allows the cell to change the highlighted of accessoryView to NO when it is pressed, but the highlighted of accessoryView is changed to YES when the finger is up and away from the cell;
Therefore, if you want to achieve highlighted of the child view on the cell when it is pressed or released, you need to understand the collaboration between TableView and TableViewCell when it is pressed.
When the finger is pressed on the cell, the system calls the following sequence method in the cell selected action.
Bytes -------------------------------------------------------------------------------------------------------------------
-(Void) setHighlighted :( BOOL) highlighted animated :( BOOL) animated UITableViewCell (when you press the cell finger) highlighted: YES
-(Void) setHighlighted :( BOOL) highlighted animated :( BOOL) animated UITableViewCell (when the cell phone leaves) highlighted: NO
TableView: willSelectRowAtIndexPath: UITableView (when the cell phone leaves the cell and the cell is selected)
-(Void) setSelected :( BOOL) selected animated :( BOOL) animated UITableViewCell (when the cell phone leaves the cell and the cell is selected) selected: YES
TableView: didSelectRowAtIndexPath: UITableView (when the cell phone leaves the cell and the cell is selected)
Bytes -------------------------------------------------------------------------------------------------------------------
In fact, the Child view on the cell will be highlighted when the cell is highlighted, because the selectedBackgroundView of UITableViewCell affects. When UITableViewCell is selected, UITableViewCell adds selectedBackgroundView as a subview. selectedBackgroundView is added to the backgroundView of UITableViewCell or all other views. When setSelected: animated: is called, selectedBackgroundView appears and disappears in an alpha-digested state.
Therefore, we can know that if the selectionStyle value of UITableViewCell is UITableViewCellSelectionStyleNone, selectedBackgroundView will not work.
We perform the following subclass to solve the problems we encountered above
@ InterfaceUCaiTableViewCell: UITableViewCell
@ End
@ ImplementationUCaiTableViewCell
@ SynthesizepiosaDelegate = _ piosaDelegate;
-(Void) setHighlighted :( BOOL) highlighted animated :( BOOL) animated {
[Super setHighlighted: highlighted animated: animated];
If (highlighted ){
[(UIButton *) self. accessoryView setHighlighted: NO];
}
}
-(Void) setSelected :( BOOL) selected animated :( BOOL) animated {
[Super setSelected: selected animated: animated];
If (selected ){
[(UIButton *) self. accessoryView setHighlighted: NO];
}
}
So far, in the case of cell highlighting, its accessoryView will not be affected and will become highlighted. The same principle applies to any subview in the cell.