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.
Related
The view on the right bottom added at the imageview , when i select the cell, the bgcolor will changed ,how avoid this happen?
thanks.
Image Before Selection
Image after Selection
Change your cell's selection style to none either programmatically or from storyboard.
If you want to do it programmatically, you can may write this code in your cell's awakeFromNib method.
cell.selectionStyle = .none
This will resolve your issue.
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;
}
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.
When you use the built-in styles (subtitle, right detail, etc) for UITableViewCells, you can access the text labels very easily with textLabel and detailTextLabel which are properties on the UITableViewCell, no matter which style you choose. I used this to my advantage to implement reusable code that allows me to apply specific styles to all of my static cells. But now I want to convert them all to a custom style cell, but with this style I still will only have two labels. My question is, is it possible to manually set the textLabel and detailTextLabel properties for a custom cell? If so, I would not have to change my code, I would just have to simply set the label properties. Otherwise, I'm going to have to change all of my code to target each individual label for each individual cell which will be really messy.
For an example of what I'm doing, I have a method that accepts in a UITableViewCell and in that method I can enable or disable that cell, which changes the labels text colors to black or light gray as appropriate. If I can't access the textLabel and detailTextLabel properties, I'm going to need to add in if statements to compare the cell parameter to my cell outlets to know which labels I need to change.
You sure can! Just implement the getters for the labels to redirect to your custom cell's labels.
- (UILabel *)textLabel {
return self.myCustomCellTextLabel;
}
- (UILabel *)detailTextLabel {
return self.myCustomCellDetailTextLabel;
}
For people using Swift:
var textLabel: UILabel? {
return myCustomCellTextLabel
}
var detailTextLabel: UILabel? {
return myCustomCellDetailTextLabel
}
In custom cell, you have to add all the views in contentView. That's the designed way, and using of existing textField, and detailTextField is not recommended because it may cause undefined behavior by built-in layout logic. (I haven't used them. They may work well. But I will not take the risk)
If you want to avoid patching all existing code, you can try subclass and overriding the properties to labels which created by you.
#interface CustomCell1 : UITableViewCell
#end
#implementation CustomCell1
{
UILabel* _your_custom_label1;
}
- (UILabel*)textLabel
{
return _your_custom_label1;
}
#end
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.