UITableViewCell transparent background (including imageView/accessoryView)

二次信任 提交于 2019-12-02 17:20:15
mgile

Ben and I figured this out today, here's the summary for the group in case this catches anybody else.

You have to set the cell background and cell.textLabel.backgroundColor every time cellForRowAtIndexPath is called, not just during the alloc/init phase (i.e. if the tableView has a dequeue cache miss).

So, the code becomes this:

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        cell.opaque = NO;        
    }

    // All bgColor configuration moves here
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor colorWithRed:.1 green:.1 blue:.1 alpha:.4];
    cell.textColor = [UIColor whiteColor];
    cell.imageView.image = [icons objectAtIndex:indexPath.row];
    cell.textLabel.text = [items objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

Have you looked into setting the background color of the cell's contentView and keeping the cell, accessory, and image views transparent?

Also, this article might help show you some alternatives.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!