change UITableViewCell background color when selected - iphone

i have a UItableview and i want to change the cell background when it is seleceted and keep it like that so i tried to use
[cell setBackgroundColor:[UIColor purpleColor]];
but that made other cells get colored at same time and i dont know why
i also tried to use
[cell.textLabel setBackgroundColor:[UIColor purpleColor]];
but when i select another cell the background go back to white color
so any ideas for solving that problem??

UIView *backgroundView = [[UIView alloc] initWithFrame:cell.selectedBackgroundView.frame];
[backgroundView setBackgroundColor:[UIColor purpleColor]];
[cell setSelectedBackgroundView:backgroundView];
[backgroundView release];

since 3.0, set it in the willDisplayCell method:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}

Subclass UITableViewCell by yourself class, i.e. MyTableViewCell and reimplement the selected function.
-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
UIView *backgroundView = [[UIView alloc] initWithFrame:self.selectedBackgroundView.frame];
[backgroundView setBackgroundColor:[UIColor purpleColor]];
[self setSelectedBackgroundView:backgroundView];
}

Related

UITableView, select cell with colour different than blue?

I've got a table view, how can I select a cell with different colour? Or how can I set the image of selection? thanks
You can easily change the cells selection style to gray:
[myTableView setSelectionStyle: UITableViewCellSelectionStyleGray];
Or you can change the contentView's backgroundColor in the willDisplayCell delegate:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}
Another way would be to create a custom cell and change the color of the Background.
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];
Presumably you could do the same with a UIImageView as well
UIImageView *bgImageView = [[UIImageView imageNamed:#"Your Image"];
[cell setSelectedBackgroundView:bgImageView];
[bgImageView release];

Problem in customize cell selection Style in UITableView in iPhone

In my application ,I am using Grouped style for TableView. In that I want to customize the cell selection Style.I want that selection Style to be red.
I am using the below code for that:
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];
By using the above code. I have a problem.Since I have taken grouped Style table , in the
selection of first and last rows, the selection is appear with sharp edged rectangle instead of
appeaing round Corners.
Can Anyone Help me in this.
Thanks in Advance.
Try this ,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SelectedCellBackground.png"]] autorelease];
}
// Cell configuration takes place here
}
add QuartzCore framework. and import QuartzCore/QuartzCore.h framework in .m file. and after that add following code in cellForRowAtIndexPath Method.
UIImageView *imv = [[UIImageView alloc]init];
imv.backgroundColor=[UIColor redColor];
[cell setSelectedBackgroundView:imv];
CALayer *layer = imv.layer;
[layer setMasksToBounds:YES];
[layer setCornerRadius:10.0];
[imv release];
Override -(void)setSelected:animated: in your subclass.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if (selected)
[self setBackgroundColor:[UIColor redColor]];
else
[self setBackgroundColor:[UIColor whiteColor]];
}
You can add fancy animation here to blend in and fade out the background upon selection and deselection if animated is YES.
In general UITableViewCell can be a bit awkward to subclass, be patient here.
UIImageView *imageVieww=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell2.bounds.size.width, cell2.bounds.size.height)];
imageVieww.image=[UIImage imageNamed:#"mavilik.png"];
[cell2 setSelectedBackgroundView:imageVieww];
[imageVieww release];

UItableviewcell textlabel background color issue

cell.textLabel.backgroundColor = [UIColor clearColor];
does not work in iPad3.2.2 but it works fine in 4.2.
can anyone help in clear the cell text background color.
what my requirement is I need to show the cell.backgroundView clearly.
thanks in advance
By default UIKit will set the backgroundColor's of all subviews when selecting it.
If you want something differently subclass UITableViewCell and add this method to your subclass:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
[[self textLabel] setBackgroundColor:[UIColor clearColor]];
[[self detailTextLabel] setBackgroundColor:[UIColor clearColor]];
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
[[cell textLabel]setBackground:[UIColor clearColor]];
}
Try this brother I hope work fine in very iOS version.
I have the same issue in an iPad running 4.3.3.
[cell.textLabel setBackgroundColor:[UIColor grayColor]];
this works to me.
Background color is a real nightmare !
good luck

How to change color of detail disclosure button for table cell

I want to change the color of detail disclosure button of a table cell. Thanks in advance.
You have to create a custom UIButton and set it as cell's accessoryView.
Your cellForRowAtIndexPath: will look something like the following,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//...
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
UIButton *myAccessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
[myAccessoryButton setBackgroundColor:[UIColor clearColor]];
[myAccessoryButton setImage:[UIImage imageNamed:#"my_red_accessory_image"] forState:UIControlStateNormal];
[cell setAccessoryView:myAccessoryButton];
[myAccessoryButton release];
//...
}
//...
}
Change the tint of your tableview and cell will work too.
You can change the Global Tint Colour. This will change the default colour to the colour you change in you app. This will change the colour of accessory button to the colour you select.
As you got best code with you from simon: what i did is:
UIButton *trackImageOnMap=[[UIButton alloc] initWithFrame:CGRectMake(420, 5, 40, 50)];
[trackImageOnMap setImage:[UIImage imageNamed:#"track_map_icon.png"] forState:UIControlStateNormal];
[trackImageOnMap setTag:iId];
[trackImageOnMap addTarget:self action:#selector(trackImageOnMapButtonTouched:)forControlEvents:UIControlEventTouchDown];
[trackImageOnMap setContentMode:UIViewContentModeScaleToFill];
[cell setAccessoryView:trackImageOnMap];
this implements some extra properties along with required one..

Help adding a backgroundView to UITableViewCell

I'm getting really desperate trying to add a UIImageView to UITableViewCell.backgroundView. All my efforts have resulted in this crappy rendering:
alt text http://img.skitch.com/20091123-ch8wk6pdxqkrn9tpftnhusigcy.jpg
It looks like the cell's label's white background is sitting on top of cell's background and covering portions of it.
I tried setting the label's background color to clear, or some other color and it does not have any event. It is always white.
The reason I know it's the text label's background causing this white area is that if I don't do [cell setText:#"Cell text here"]; the white area is gone and I see just the cell's background image.
Here's the code that I'm using. The table view is added in the .xib file and UITableView is added to UIViewController:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [myCollection.items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger rowIndex = indexPath.row;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"darkCellBackground.png"]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"darkCellBackground.png"]];
}
[cell setText:#"Cell text here"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:#"AnotherView" bundle:nil];
// [self.navigationController pushViewController:anotherViewController];
// [anotherViewController release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
I'm sure I'm doing something wrong but cant quite figure out what.
The solution is here: Changing UITableViewCell textLabel background color to clear
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
[[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];
}
What you want is:
cell.backgroundColor = [UIColor colorWithPatternImage:[[UIImage imageNamed:#"some.png"]stretchableImageWithLeftCapWidth:320 topCapHeight:44]];
Did you try setting the cell's textLabel to not be opaque?
cell.textLabel.opaque = NO;
Or, if your background is a solid color where the label is, you could set the label's background color to the appropriate color.
Deprecation sidenote:
[UITableViewCell setText:] is deprecated in OS 3.0 - you shouldn't use it (if you're building for 3.0+) as it could disappear in the future. Instead, you should use UITableViewCell's textLabel property to set the text of the UILabel directly.
Memory management sidenote:
You're leaking two instances of UIImageView for every UITableViewCell you create using this code. These lines
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"darkCellBackground.png"]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"darkCellBackground.png"]];
should be changed to this
cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"darkCellBackground.png"]] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"darkCellBackground.png"]] autorelease];
Or you could use release instead of autorelease (doing it properly; don't try to just substitute release for autorelease, of course). Either way, the UITableViewCell takes ownership of the background views and because you alloc'd the objects you need to release your ownership of the objects.
Maybe you should take a look at this:
How can I set the background color of a cell in UITableView on iphone?
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"list-view-bg.png"] highlightedImage:[UIImage imageNamed:#"list-view-bg.png"]];
}
Set the table view background to clear color