I got an app with UITableView. Every cell has accessory. But i cant set backgroundColor ti this cells.
I tried to set backgrounColors in this ways:
cell.contentView.backgroundColor =[UIColor redColor];
cell.backgroundView.backgroundColor = [UIColor redColor];
cell.backgroundColor = [UIColor redColor];
But only contentView work. How can i do it? Thnx
the second way cell.backgroundView.backgroundColor = [UIColor redColor]; is not that wrong. Unfortunately there is no backgroundView until you create one. You have to create an UIView and set it as background of the cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"Cell"];
UIView *background = [[UIView alloc] initWithFrame:CGRectZero];
background.backgroundColor = [UIColor redColor];
cell.backgroundView = background;
}
Related
I'm working with RESIDEMENU,and trying to add a line between cells of LeftMenuViewController,here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:21];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
cell.selectedBackgroundView = [[UIView alloc] init];
}
NSArray *titles = #[#"Home", #"Calendar", #"Profile", #"Settings", #"Log Out"];
NSArray *images = #[#"IconHome", #"IconCalendar", #"IconProfile", #"IconSettings", #"IconEmpty"];
cell.textLabel.text = titles[indexPath.row];
cell.imageView.image = [UIImage imageNamed:images[indexPath.row]];
UIView * lineView= [[UIView alloc]initWithFrame:CGRectMake(10, 0, cell.contentView.bounds.size.width, 3)];
lineView.backgroundColor=[UIColor redColor];
[cell.contentView addSubview:lineView];
return cell;
}
I can see these lines at first, but when I touch any cell, the cell is highlighted ,and the line of this cell disappear weirdly.any way to fix it?
snapshot before :
snapshot when click a cell:
UITableViewCell changes the background color of all sub views when cell is selected or highlighted.
You have three options :
No selection style
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Subclass UITableViewCell and overriding Tableview cell's setSelected:animated and/or setHighlighted:animated
Add the line as a layer
CALayer* layer = [CALayer layer];
layer.frame = CGRectMake(10, 0, cell.contentView.bounds.size.width, 3);
layer.backgroundColor = [UIColor redColor].CGColor;
[cell.contentView.layer addSublayer:layer];
Another workaround is overwriting setBackgroundColor: and expose similar method to change its backgroundColor.
#interface
- (void)setPersistentBackgroundColor:(UIColor*)color;
#implementation
- (void)setPersistentBackgroundColor:(UIColor*)color
{
super.backgroundColor = color;
}
- (void)setBackgroundColor:(UIColor *)color
{
// [super setBackgroundColor:color];
// prohibit from changing its background color.
}
I need to make the background of only one UITableViewCell fully transparent for iOS7 / iOS6.
What I did:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ButtonsCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView = [UIView new];
cell.selectedBackgroundView = [UIView new];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
What I get:
in iOS6 everything is OK:
but in iOS7 there are two horizontal grey lines:
Is it possible to remove them?..
tableView.separatorColor = [UIColor clearColor];
I am using a slide in menu style which loads a UITableView. - ECSlidingViewController
I have about 7 cells in a table view setup as follows:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.contentView.backgroundColor = [UIColor colorWithRed:75.0/255.0 green:83.0/255.0 blue:102.0/255.0 alpha:1.0];
UIView *topSplitterBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 1)];
topSplitterBar.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:69.0/255.0 blue:85.0/255.0 alpha:1];
[cell.contentView addSubview:topSplitterBar];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor colorWithRed:196.0/255.0 green:204.0/255.0 blue:218.0/255.0 alpha:1];
cell.textLabel.font = [UIFont systemFontOfSize:18.0f];
cell.textLabel.shadowColor = [UIColor colorWithRed:27.0/255.0 green:31.0/255.0 blue:41.0/255.0 alpha:1];
cell.textLabel.shadowOffset = CGSizeMake(0, 1);
UIView *selectedBg = [[UIView alloc] initWithFrame:cell.frame];
selectedBg.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1];
cell.selectedBackgroundView = selectedBg;
What would be the best way to show a cell as the selectedBg if that is the currently displayed controller?
I can access the following for example:
if ([self.slidingViewController.topViewController isKindOfClass:[MESHomeViewController class]]) {
However, I am not sure where would be best practice to set this up? I can do it in the switch case for the cell label setup... For example:
switch ( indexPath.row ) {
case 0: {
if ([self.slidingViewController.topViewController isKindOfClass:[MESHomeViewController class]]) {
cell.contentView.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1];
}
cell.textLabel.text = NSLocalizedString(#"LMGames", #"Left Menu - Games");
break ;
However, when a new item is selected from the menu I would need to reload the table each time, is that good? Completing a self.tableView reloadData each time a cell is selected, or is there a better way to approach this?
Two ideas for you:
Set the selectedBg in the didSelectRowAtIndexPath method in order to set the selected cell.
Keep an integer reference to the currently selected row and the previously selected row and then refresh only those rows by using a method similar to: How to reload and animate just one UITableView cell/row?
I hope that helps!
Searching thorugh some posts I have tried without success to set the background of some dynamically generated cells to transparent.
In the storyboard I have a prototype table, under which I have an image. In storyboard I have set alpha to 0.3 for the prototype table, so that the image is partially visible. It renders as desired.
The problem is that the cells add an extra layer of color, so that the image is still visible, but a little less (as if the alpha was 0.6 for example). Actually, it seems like there are three layers. The table, the cell (which darkens the bg a little) and then another small rectangle inside each row around the text (which darkens the bg even more).
I've edited the function that generates the cells like so (based on what I've understood from the threads on this topic):
NSString *cellIdentifier = #"ItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.textLabel.text = [self.labels objectAtIndex:indexPath.row]; // gets the text of the cell from an array
cell.textLabel.textColor = [UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.0];
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
// I've also trued with = [UIColor colorWithRed:0. green:0 blue:0 alpha:0.0]
cell.backgroundView = backView;
The problem is that I still get that extra layer.
See, I have done changes somewhere in your code and try to use it.
[tableView setOpaque:NO];
NSString *cellIdentifier = #"ItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//if cell is already created then reuse it no need to create unnecessarily.
if(cell= nil){//otherwise create new cells.
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
}
cell.textLabel.text = [self.labels objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.0];
[tableView setOpaque:NO];
[tableView setBackgroundColor:[UIColor clearColor]];
Im adding 2 texts into a cell of a table. What I am doing is
UITableViewCell *cell =nil;
NSString *CellIdentifier = #"Cell";
cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
[[UILabel alloc] initWithFrame:CGRectMake(80.0, 0.0, 220.0, 10.0)];
mainLabel.tag = 1003;
mainLabel.text = #"Text 1";
mainLabel.textAlignment = UITextAlignmentLeft;
mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:mainLabel];
}
I do the same thing to add secondLabel to a cell.
What is displaying on the simulator is
Now the problem is the background of 2 UILabel is not the same with the background of the cell ( my cells are grouped in the table view )
Does anyone know how to fix this problem.
Any comments are welcomed here
Thanks
If I have understand well the question, you need to delete the UILabel background because you want to show the UITableViewCell background instead. So I think you only have to do that:
mainLabel.backgroundColor = [UIColor clearColor];
Like the answer above, you should set the same color for both of the view.
cell.backgroundColor = [UIColor clearColor];
or you can set the color of the View you set.