UITableView Selected Row Changes Subview Background Color - ios

I have a UItableview with custom cells in it. The height of the cell changes when you select it and gives an expanding effect. However, when you you select the cell the background of all the subviews become transparent it seems. I've tried setting the cell's SelectedBackgroundView but that doesn't really affect the cells subviews.
Here are some images:
Closed:
Open:
![enter image description here][2]
This is how its supposed to look or at least does in XCode - (Sorry for the bad graphic here)

Call [tableView deselectRowAtIndexPath:indexPath animated:YES]; at didSelectRowAtIndexPath. This should solve your issue.
Edit
If you don't want to see any grey selection at all, then, in your cellForRowAtIndexPath, set the cell.selectionStyle to UITableViewCellSelectionStyleNone, like so:
cell.selectionStyle = UITableViewCellSelectionStyleNone;

Presuming that you have subclassed UITableViewCell for your custom cells, you can modify a cell's appearance when selected/deselected by overriding the setSelected method in your custom subclass. For example:
- (void) setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
//Configure the selected state here
} else {
//Configure the deselected state here
}
}

UITableViewCell changes the backgroundColor of all subviews on selection for some reason.
This might help:
DVColorLockView

If you want the normal selection behavior but want to exclude specific cell subviews, you can do something like this to lock the background color by subclassing and checking if it is locked in the backgroundColor setter.

Related

Disable subviews isHighlighted from UITableViewCell

I have images inside of a UITableViewCell, I was using the "isHighlighted" field to set if the image should be gray or colored. Whenever I select the cell, the UITableViewCell sets isHighlighted to true on all subviews, is there any way to disable this behavior?
Sounds like you are overloading isHighlighted to do some custom logic like how to present the cells when they are not selected.
If so, it would be better to subclass UITableViewCell and add your custom property to the subclass instead of using 'isHighlighted' and use the derived class in the table.
Create a subclass with self.selectionStyle = UITableViewCellSelectionStyleNone; and then override setHighlighted:animated::.
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
[super setHighlighted:highlighted animated:animated];
// Set your imageView background color here
}
Note that UIImageView does not inherit from UIControl and so it has no highlighted property. However, UITableViewCell does try to set its subviews' background colors to transparent when it is highlighted. You may not be able to control the background color behavior of the built in imageView via setHighlighted..., in which case you would want to add your own UIImageView which you control completely.

How can I prevent blue highlight when select row from tableView.allowsMultipleSelectionDuringEditing = YES

I have a table view and custom TableViewCell with configure to allow multiple cell to be selected in editing mode and a check-mark will showing when I selected cell.
tableView.allowsMultipleSelectionDuringEditing = YES
I want to prevent the blue highlight when I selected my cell in editing mode.
I have try many ways like configure cell selection style but with this configure the "check-mark" won't be show in editing mode when cell selected.
cell.selectionStyle = UITableViewCellSelectionStyleNone;
I really need helps in this case. Thanks.
You can overwrite the selectedBackgroundView property of your custom tableViewCell
this line of code works for me
[cell setSelectedBackgroundView:[[UIView alloc]initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.x, cell.frame.size.width, cell.frame.size.height)]];
One method is to overwrite setHighlighted:animated: in your cell:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
// do nothing
}
This will block the highlighting behavior, not the selection behavior. The selection can be completely overriden using:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
// update UI for selection state
}
Swift:
cell.selectedBackgroundView = UIView(frame: cell.frame) or
cell.selectedBackgroundView.isHidden = true
But it may still not work as you expect. When selected all of the cell's subviews' backgroundColor to clear color(transparent), you may want to recover it

Don't highlight UIImageViews in UICollectionViewCell

I have a subclass of UICollectionViewCell that contains some UIImageViews that have highlighted image states. I do not want these images to become highlighted when the user taps on the collection view cell. The highlighted states are set in code based on other factors.
How can I get the cells to not set the image views to highlighted when the cell is selected?
Override setHighlighted in your UICollectionViewCell class:
-(void)setHighlighted:(BOOL)highlighted{
[super setHighlighted:highlighted];
self.imageView.highlighted = NO;
}

Make UITableViewCell behave like a real button

I want to make UITableViewCell to behave like real button.
Until know I have been using the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath`
trick, but this is not optimal because it doesn't behave like a button on tap/drag/release.
If you tap a cell row and drag your finger over the cell, it will not get selected when you release your finger (but a button would launch its action in the same case).
Is there any simple way of making a UITableViewCell to behave like a real button without resorting to insert an actual UIButton inside the cell?
You can just create table view cells with a button in them, set the buttons tag to the row so you can workout which row the button belongs to when you receive the button event. Just make sure you reset the buttons tag when you return a reused table view cell instead of creating a new one.
Subclass the UITableViewCell and use the following method:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
_backgroundImageView.image = [UIImage imageNamed:#"img_h"];
}
else {
_backgroundImageView.image = [UIImage imageNamed:#"img"];
}
}
Where img is a plain image and img_h is a highlighted version of that image.
One way is to create a UIButtton of size of your cell and added it to the cell.
Or else you could simply add a UITapGestureRecognizer to your UITableViewCell and that will do the work for you.

Cells not getting selected in UITableView with allowsMultipleSelectionDuringEditing set in edit mode

I have a UITableView that is configured to allow multiple cells to be selected in edit mode. However, the empty white circles on the left never change to red circles with the white checkmarks inside after a cell is touched/selected.
I have read about the swipe to delete issue with allowsMultipleSelectionDuringEditing, so my setEditing:animinated method looks like this:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
self.tableView.allowsMultipleSelectionDuringEditing = editing;
[super setEditing:editing animated:animated];
}
Some resources on the Net suggest setting allowsSelectionDuringEditing = NO;, but that has no effect. Also, my cell editing style is set to UITableViewCellEditingStyleDelete, and changing it does not have any effect either.
When a row is touched in edit mode, tableView:didSelectRowForIndexpath: is triggered, but as mentioned above, the UI does not reflect this.
It was, as tends to be the case, my mistake.
The problem was in my implementation of tableView:cellForRowAtIndexPath:, where I was setting the cell's selectionStyle property to UITableViewCellSelectionStyleNone. For some reason, this has the added 'benefit' of disabling the red checkmark on the left hand side in multiselection edit mode.
Setting cell.selectionStyle = UITableViewCellSelectionStyleGray; fixed the issue.
Old thread but i also had this issue, however i found the cause to be my custom cell was overriding the setSelected and setHighlighted methods without call super.
This resulted in the cells not becoming selectable.
I had a similar problem, because I was working very hard to make sure my cells never showed any selection (for a "chat bubble" style of table).
So of course this fix resulted in great big color bars on my table, and I had to find another way to get rid of them.
In your cellForRowAtIndexPath, you can set a selectedBackgroundView instead of setting the selectionStyle, and it will also enable checkboxes. The view can be your cell's background color, or clearColor, and then nothing will show up. Here's my code:
static dispatch_once_t onceToken;
static UIView * selectedBackgroundView;
dispatch_once(&onceToken, ^{
selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
selectedBackgroundView.backgroundColor = APP_CELL_BACKGROUND_COLOR;
});
cell.selectedBackgroundView = selectedBackgroundView;
I had the same problem. Ensure that tableView:shouldHighlightRowAtIndexPath: returns YES, otherwise selection will not occur, at least in iOS 7.
- (BOOL)tableView:(UITableView *)tableView
shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}

Resources