简体   繁体   中英

How to change tableViewCell background color

NOTE: this is about a cell in GROUPED tableView . That makes a HUGE difference, when compared to normal tableView! The default cell customization answers do NOT work in this case, so please verify your answer first.

This is how I set gray screen and yellow tableView background:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    UIView *myView = [[UIView alloc] initWithFrame:magicRect];
    myView.backgroundColor = [UIColor yellowColor];
    [self.tableView addSubview:myView];
    [self.tableView sendSubviewToBack:myView];
}

This is how I set green cell background. As you can see from picture, it's missing some areas:

- (UITableViewCell *)tableView:(UITableView *)tableView
   cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 UITableViewCell *cell =
   [tableView dequeueReusableCellWithIdentifier:@"Cell"];
 if (cell == nil) {
   cell = [[[UITableViewCell alloc]
     initWithStyle:UITableViewCellStyleValue1
     reuseIdentifier:@"Cell"] autorelease];
   cell.backgroundColor = [UIColor greenColor];
 }
 // Configure the cell...
}

Question: how can I change color at start and end of tableView cell ? Now the cell is transparent in those areas and displays self.view.backgroundColor from below the whole tableView. Those areas really are transparent, since textured background remains in same location, when scrolling tableView.

在此处输入图片说明

set tableView Background as clear color like this,

- (UITableViewCell *)tableView:(UITableView *)tableView
   cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

     [tableView setBackgroundColor:[UIColor clearColor]];//Here you can give as yellow instead of adding view
     //ur code
}

我不知道为什么要向表视图添加子视图,可以为表视图设置背景颜色:

tview.backgroundColor=[UIColor yellowColor];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM