I'm using Async Display Kit to display cell nodes in an ASTableView. How can I set a custom color for the cell node's selected state. With normal tableView cells I would just override
(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
in my cell implementation, but that method doesn't exist on ASCellNodes. Has anyone else encountered this problem and how did you solve it?
Assuming you've subclassed ASCellNode to create your own cells, you could just add your own setHighlighted method.
e.g.
- In Your ASCellNode Subclass
- (void)setHighlighted:(BOOL)highlighted {
if (highlighted) {
self.backgroundColor = [UIColor blueColor];
} else {
self.backgroundColor = [UIColor whiteColor];
}
}
- In Your Delegate Implementation
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MyNodeSubclass * node = (MyNodeSubclass *)[(ASTableView *)tableView nodeForRowAtIndexPath: indexPath];
[node setHighlighted: YES];
}
Note:
You'll need to maintain your own state in regards to which cells are selected/deselected
Related
Following the advice in this answer Custom UITableViewCell selection style? about how to make a custom selection style for a UITableViewCell I did the following.
First, in my custom view controller, which has a UITableView as a property and which serves as its datasource and delegate I have the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//UITableViewCell *cell;
NavTableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Then this is my entire custom UITableViewCell:
#import "NavTableViewCell.h"
#implementation NavTableViewCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if(highlighted){
self.contentView.alpha = .5;
}else{
self.contentView.alpha = 1;
}
}
#end
I have also tried putting the code in setSelected. In both cases I don't see any change in my selected/highlighted table view cell. I have tried changing its color and changing the colors of the labels within it, but there is simply no change. I have also tried going through all the kinds of selection styles for the cell, all to no avail as far as seeing my customization.
What am I doing wrong?
From the documentation:
You can use selectedBackgroundView to give the cell a custom appearance when it is
selected. When the cell is selected, this view is layered above the
backgroundView and behind the contentView.
So whatever you want to do to your cell while selecting it, you have to do it for the selectedBackgroundView .
Try this:
UIView *highlightView = [[UIView alloc] init];
highlightView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:1.0 alpha:.5];
cell.selectedBackgroundView = highlightView;
My Tableview cells expand when tapped, to show more information in 3 UITextViews.
But the cell changes color (as i want it to), and this hides the UITextViews, but not the other subviews as you can see in the image below.(orange lines indicate the textviews).
Changing the color of the textview background makes no difference.
What could i try?
EDIT ------ new image with TextFields.
This issues caused by UITableViewCell selection. When you select cell it changes backgroundColor on all subviews. To prevent this you have two options:
1) Subclass UITableViewCell and set color to subviews in:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
2) Setup color in tableView:didSelectRowAtIndexPath::
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell isSelected]) {
UIView *view = [cell viewWithTag:TEXTFIELD_TAG];
view.backgroundColor = [UIColor greenColor];
}
}
If i have not misunderstood the question then You can try adding all these three text view in one uiview(container) . Then on didselect table view cell change the background color of that container view. Hope it helps.
Have your cell selection style to none and override setSelected method of tableView cell to change background color of cell.
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
#implementation CustomTableViewCell
-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
//--
self.contentView.backgroundColor = (selected) ? [UIColor grayColor]:[UIColor whiteColor];
}
#end
Can anyone enlighten me on how to make the uitableview footer have a clear background?
I have added a 15 height footer which is added via the following -
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
if (section==0){
return 0;
}
else{
return 15;
}
}
that works fine - but the footer is a grey strip - the tableview has a background image - so i'd like the footer to be clear - is this possible? if so how do i style it?
We have a better solution for this from iOS6 onwards:
There is a delegate method - (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
You can use this method like this
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section {
//Set the background color of the View
view.tintColor = [UIColor blueColor];
//Set the TextLabel Color
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor whiteColor]];
}
Implement the - (UIView *)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section delegate and return a UIView in it with clear background.
You could also try setting the [self.table setTableFooterView:nil]
That should solve the issue.
I have a UICollectionView with labels inside the cells that change automatically periodically. When this update triggers I call reloadData on the UICollectionView and I have set the cells to change the background colour on [UICollectionViewCell setHighlighted:].
The problem is if a user holds down on a cell then the update happens, when the user releases the cell stays highlighted and also cannot be selected anymore.
I have noticed that dequeueReusableCellWithReuseIdentifier:forIndexPath: calls setHighlighted on the cells after reloadData.
I have also tried reloadSections: instead of reloadData, this fixes the problem of the cells getting 'stuck', but causes a fade out and in on the cells when ever its called.
Placing the calls inside performBatchUpdates: doesn't seem to fix the problem either.
Inside the cell's class try calling:
- (void)prepareForReuse {
[super prepareForReuse];
[self setHighlighted:NO];
... Any other custom stuff that should be cleaned up ...
}
The issue is that you are probably doing background coloring differently then the cell would normally do on its self when highlighted. Normally the cell's superclass would undo those changes in prepareForReuse, but it doesn't know about your changes.
I used the following as a workaround:
// Both highlightedData and lastHighlightedData are only needed if you want to prevent user from selecting a cell which data changed during the reload. If not needed, a boolean may be used instead
#property (nonatomic) id highlightedData;
#property (nonatomic) id lastHighlightedData;
#property (nonatomic) BOOL pendingCollectionViewReload;
// Wrap the reloadData call. Enqueue it if there's a highlighted cell:
- (void)reloadCollectionView
{
if (self.highlightedData) {
self.pendingCollectionViewReload = YES;
return;
}
[self.collectionView reloadData];
self.pendingCollectionViewReload = NO;
}
// When a cell is highlighted, save its index, or better the related data:
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
// Save data at indexPath to self.highlightedData
}
// Then reload the data when the cell is unhighlighted:
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
self.lastHighlightedData = self.highlightedData;
self.highlightedData = nil;
if (self.pendingCollectionViewReload) {
[self reloadCollectionView];
}
}
// The following can be used from shouldPerformSegueWithIdentifier or didSelectItemAtIndexPath to prevent actions if the data associated with the indexPath changed:
- (BOOL)selectedDataEquals:(id)data
{
// I used lastHighlightedData in addition to highlightedData because this function may be used after the didUnhighlightItemAtIndexPath was called:
return (self.highlightedData && self.highlightedData == data) || (!self.highlightedData && self.lastHighlightedData == data);
}
I encountered the same problem in a collection view that didn't need highlighting and I noticed that the didHighlight/didUnhighlight methods behaved correctly, so I ended up blocking reloads while a touch was in progress, using something like this:
BOOL blockColviewUpdate,colviewUpdateQueued;
and then
-(void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
blockColviewUpdate=YES;
}
-(void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
blockColviewUpdate=NO;
if(colviewUpdateQueued==YES) [self CollectionViewRefresh];
}
while using an own function instead of calling reloadData directly:
-(void)CollectionViewRefresh
{
if(blockColviewUpdate==YES) colviewUpdateQueued=YES;
else
{
colviewUpdateQueued=NO;
[self.colview reloadData];
}
}
It helped in my case and no reloads were lost.
I am using collection views in one of my recent projects and the following code works fine for me.
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath: (NSIndexPath *)indexPath
{
return YES;
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;
{
return YES;
}
In the collectionviewcell subclass
#interface SalesLegendCell : UICollectionViewCell
below is the code
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:#"SalesLegendCell" owner:self options:nil];
if ([arrayOfViews count] < 1) {
return nil;
}
if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[SalesLegendCell class]]) {
return nil;
}
self = [arrayOfViews objectAtIndex:0];
UIView* backgroundView = [[UIView alloc] initWithFrame:self.bounds];
backgroundView.backgroundColor = [UIColor colorWithWhite:0.85 alpha:1];
UIView* selectedBGView = [[UIView alloc] initWithFrame:self.bounds];
selectedBGView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1];
self.selectedBackgroundView = backgroundView;
self.backgroundView = selectedBGView;
}
return self;
}
Possibly unrelated, but this bit me: UIImage instances rendered on a UIViewCollectionCell in .alwaysTemplate mode get by default highlighted.
I'm trying to set the color of the selected row in a UITableView, using this code:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}
}
The problem is, it's setting the color on all the rows. Furthermore, when I do select a specific row, it highlights that row in blue. How do I get it to just highlight the selected row, in red?
The easiest way to me is to subclass UITableViewCell and overwrite the setSelected: method (leave everything else the same if you need to)
some sample code from an old project:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
UIView *bg = [[UIView alloc] initWithFrame:self.frame];
bg.backgroundColor = [UIColor colorWithRed:.916 green:.9648 blue:.9844 alpha:1.0];
self.selectedBackgroundView = bg;
[bg release];
// Configure the view for the selected state
}