UITableViewCell透明背景(包括imageView/accessoryView)

Ben*_*man 10 iphone uitableview iphone-sdk-3.0

当我将UITableViewCells设置为backgroundColor半透明颜色时,它看起来不错,但颜色并不覆盖整个单元格.

周围的区域imageViewaccessoryView涌现出来的[UIColor clearColor]...

替代文字

我已经尝试明确设置cell.accessoryView.backgroundColorcell.imageView.backgroundColor与单元格相同的颜色backgroundColor,但它不起作用.它在图标周围放置一个小盒子,但不会扩展以填充左边缘.右边缘似乎不受此影响.

我怎样才能解决这个问题?

编辑:这是原始表格单元格代码:

- (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;
        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;
}
Run Code Online (Sandbox Code Playgroud)

小智 16

Ben和我今天想出了这个,这是该小组的摘要,以防其他人抓到.

您必须设置单元格背景并且cell.textLabel.backgroundColor每次都cellForRowAtIndexPath被调用,而不仅仅是在alloc/init阶段期间(即,如果tableView具有出列缓存未命中).

所以,代码变成了这样:

- (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;
}
Run Code Online (Sandbox Code Playgroud)