【问题标题】:iOS: TableView in nib - set background colouriOS:笔尖中的 TableView - 设置背景颜色
【发布时间】:2013-09-30 19:43:24
【问题描述】:

编辑:描述时我不清楚。我正在加载的 UITableView 不是 UITableView 的 xib - 它是包含 UITableView 的自定义 UIView(并且已正确连接代表和插座等)

我正在尝试从从笔尖加载的表格视图中设置 UITableViewCell 的背景颜色。

首先我尝试将 tableView 背景颜色属性设置为 UIColor clearColor。那没有用,所以我像这样更改了我的 cellForRowAtIndexPath:

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

static NSString *CellIdentifier = @"scoreCell";

UITableViewCell *cell = [tableView
                         dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc]
            initWithStyle:UITableViewCellStyleSubtitle
            reuseIdentifier:CellIdentifier];
}

//First try
cell.backgroundColor = [UIColor clearColor];

//Added this after the others didn't work
cell.contentView.backgroundColor= [UIColor clearColor];

//Added this after
cell.textLabel.backgroundColor  = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
// Configure the cell.

cell.textLabel.text = [[leaderInfo objectAtIndex:indexPath.row] objectForKey:@"name"];
cell.detailTextLabel.text = [[[leaderInfo objectAtIndex:indexPath.row] objectForKey:@"score"] stringValue];
cell.textLabel.adjustsFontSizeToFitWidth  =YES;


return cell;

}

然后根据我尝试过的文档,这不起作用:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//Properties tested in all permutations
    cell.backgroundColor=[UIColor clearColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}

【问题讨论】:

    标签: ios cocoa-touch uitableview


    【解决方案1】:

    要设置单元格颜色,需要将tableView的背景颜色设置为clear,并将其backgroundView设置为nil。

    - (void)viewDidLoad
    {
        [self.tableView setBackgroundColor:[UIColor clearColor]];
        [self.tableView setBackgroundView:nil];
    }
    

    【讨论】:

    • 虽然 nib 中没有 viewDidLoad,但想法是对的。我在视图本身加载之前进行设置。
    猜你喜欢
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 2018-01-06
    • 1970-01-01
    相关资源
    最近更新 更多