UItableviewcell textlabel background color issue - iphone

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

Related

Change backgroundColor of sectiontitle in UITableView

I have a UITableView with sections and the title for them I m setting in titleForHeaderInSection like this:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringwithFormat :#"Section %d",section];
}
Defaulty it comes with graycoloured background with textcolor white.How can I change it to some other colour.?
Gone through google and found to set in viewForHeaderinSection.
But I did't use it to setheaders .So I dont want to write in it.
How can I write it in titleForHeaderInSection?
In iOS 6 and later
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setFont:[UIFont systemFontOfSize:18.0f]];
[[UIView appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setBackgroundColor:[UIColor redColor]];
titleForHeaderInSection : This delegate of UITableView is used to set only text. You can see below it's return type it NSString.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// fixed font style. use custom view (UILabel) if you want something different
}
In order to achieve the customized view for a section header your only option is viewForHeaderInSection delegate method. I can assure there's no way you can set the view for header using titleForHeaderInSection Also, viewForHeaderInSection acts exactly same as titleForHeaderInSection You can see implement it as below :
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//Customized your view however you want.
return myCustomizedView;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection: (NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
if (section == integerRepresentingYourSectionOfInterest)
[headerView setBackgroundColor:[UIColor redColor]];
else
[headerView setBackgroundColor:[UIColor clearColor]];
return headerView;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *tempView=[[[UIView alloc]initWithFrame:CGRectMake(0,0,300,25)]autorelease];
tempView.backgroundColor=[UIColor grayColor];
UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,0,300,25)];
tempLabel.backgroundColor=[UIColor clearColor];
NSString * headerText = [NSString stringWithFormat:#"%d",section];
tempLabel.text= headerText;
[tempView addSubview: tempLabel];
[tempLabel release];
return tempView;
}
And
- (float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 25;
}

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

Changing background color of a custom UITableViewCell cell in a plain styled UITableView

I am trying to change the background color of a custom UITableViewCell in a plain style UITableView.
I've read the other similar questions, and I in my delegate method:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
I set cell.backgroundColor = [UIColor...]. The problem is this only works for a grouped style UITableView.
Note: The reason why I want to do this programmatically and not in my .xib file is because I want different background colors for different cells.
Maybe you can add cell.contentView.backgroundColor = [UIColor clearColor] before you set your color.
The reason is that the backgroundview is at the bottom. The contentview is at the top.
Change the color in the following delegate method:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (...){
cell.backgroundColor = [UIColor blueColor];
} else {
cell.backgroundColor = [UIColor whiteColor];
}
}
// set selection color
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1];
[cell.contentView addSubview:myBackView];
[myBackView release];
like this change every cell as u needed

change UITableViewCell background color when selected

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

UITableView multiple selection

I have a UITableView that I want to use to allow multiple selections. I have rolled together most of the functionality, but am having one small problem.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Selected");
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
[selectedCell setBackgroundColor:[UIColor grayColor]];
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[[selectedCell textLabel] setTextColor:[UIColor whiteColor]];
} else {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[selectedCell setBackgroundColor:[UIColor clearColor]];
[[selectedCell textLabel] setTextColor:[UIColor blackColor]];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
This is the code I am using to keep the cell selected, however when the user taps on the cell, the aaccessory view turns white and then turns back to blue.
What is the best way for me to keep the accessory view white?
Thanks
I believe easy way to achieve this is by creating custom imageview with checkmark image(you need to get it as white checkmark) and set it as accessory view when selected.
This will avoid the standard behavior of the accessoryview and achieve your goal.