如何在ios6和ios7中管理UITableView背景色



查看此代码。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellIdentifier = @"CellIdentifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    [self setUpCell:cell withIndexPath:indexPath];
}
[self updateCell:cell withIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[[cell  imageView] setImage:[UIImage imageNamed:[[data objectAtIndex:[indexPath row]] valueForKey:@"image"]]];
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[tableView setBackgroundColor:[UIColor clearColor]];
[tableView setBackgroundColor:_kColorFromHEX(@"#146f50")];
[cell setBackgroundColor:_kColorFromHEX(@"#146f50")];
return cell;
 }

为什么将UITableView的背景色设置为cellForRowAtIndexPath ?通常情况下,您希望这段代码只执行一次,并表示将这段代码放置在viewControllerviewDidLoad方法中可能是最好的位置。

下面是一个示例

-(void)viewDidLoad
{
 [super viewDidLoad];
 [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
 [tableView setBackgroundView:nil];
 [tableView setBackgroundColor:_kColorFromHEX(@"#146f50")];
}

要更改tableview的背景色,请使用

先试试这个

[UITableView setBackgroundView: nil];

或者这个

UIView* bview = [[UIView alloc] init]; 
bview.backgroundColor = [UIColor yellowColor];
[tableView setBackgroundView:bview];

要更改tableview单元格背景颜色,请使用这个委托方法

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{
    cell.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
}

希望这对你有什么作用

最新更新