Don't highlight UIImageViews in UICollectionViewCell - ios

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;
}

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.

Detecting taps on Custom Cells of UITableView

So i have a custom UITableViewCell which contains three UILabel. The UITableViewCell is such that the UILabel completely cover the cell.
Now i want to detect whenever user taps on the cell. Problem is as the UILabel cover the cell I cannot use UITableView delegate method for detecting touch on the cell.
I thoughout about using gesture recoginzers on the UILabel but then i don't get the index of the touched cell. I also thought about placing a transparent button on top of the cells but here there is also the same problem that i can't get the index of the touched cell.
Can anybody guide me with an approach on how can i accomplish detecting taps with tableView didSelectRowAtIndexPath when UILabel are covering the cell.
Try to set userInteractionEnabled to false in all labels in cell. This will pass touch to the cell and then you can use UITableView delegates. Be sure that cell stays userInteractionEnabled = YES
To get the touch event besides didSelect method try this. In your custom cell class add a block like,
typedef void(^TappedCell)(CustomCell *cell);
and add a property for it,
#property (nonatomic, strong) TappedCell tappedCell;
and on transparent button action
- (IBAction)buttonTapped {
if (self.tappedCell) {
self.tappedCell(self);
}
}
And in cellForRow method
__weak type(self)weakSelf = self;
cell.tappedCell = ^ (CustomCell *tappedCell) {
//Ur actions goes here
//for getting index
NSIndexPath *indexPath = [weakSelf.tableView indexPathForCell:tappedCell];
[weakSelf someActionWithIndexPath:indexPath];
};
It doesnt matter whether UILabel hide your cell or UIImageView hide it. It will automatically detects tableView didSelectRowAtIndexPath. Check it first you wont face any problems. Did you try to implement tableview in detail first

UITableView Selected Row Changes Subview Background Color

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.

Strange behaviour of Subclassed UITableViewCell when selected

I have a subclass of UITableViewCell, which has a number of UILabels, a UIImageView and a UIView in it.
I have the cell set to highlight blue when it is selected. The strange behaviour that I am observing is the UIView that I have added as a property of the UITableViewCell subclass disappears when the cell is selected, and returns when the cell is deselected. This UIView is just an empty view with a background color, so it is possible that the background color is being set to clear when the table view cell is selected. None of the other elements are effected.
Does anyone have an idea what is happening here, and how to fix it?

Changing the contentView when an Accessory button is tapped

I've subclassed UITableViewCell to display a UIImage and two UILabel subviews. In the view controller for the table view, in the method cellForRowAtIndexPath: I've enabled an accessory view via setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton.
Cells display correctly.
When I tap on the accessory disclosure button I want to replace the two label subviews with two different label subviews. My approach was the following:
In the subclassed UITableViewCell, inside layoutSubviews, create the
CGRects for the "alternate" labels, position them in the same
places as the two "original" label and hide them via setAlpha:;
When the disclosure button is tapped swap out the set of two
label by adjusting their respective alphas.
The problem is I can't figure out what logic in layoutSubviews I'd use to know whether the accessory button has been tapped. I see that in the view controller accessoryButtonTappedForRowWithIndexPath: is called when that button is tapped and from there it seems like I would call layoutSubviews but can't figure out how to use that fact to accomplish what I'm trying to do.
Am I going about this all wrong? Instead of hiding/showing CGRects with alpha should I simply be creating another subclass of UITableViewCell?
When I tap on the accessory disclosure button I want to replace the two UILabel subviews with two different UILabel subviews.
I'll do the following. Create a public method in your UITableViewCell subclass like the following:
- (void)changeContentForCell;
In this method you could set the contentView as you prefer.
Then in your view controller implement
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cCell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
[cCell changeContentForCell];
}
This is a simple example for change the content. In my opinion you don't have to use layoutSubviews to add views.
Leave this logic in changeContentForCell and then call setNeedsLayout to change your layout. Maybe you could have a variable that tracks the state for your cell: normal state or modified state.
Hope it helps.

Resources