细胞背景颜色仅示出了对文本

问题描述:

我已经作为走得更远,使用以下代码来设置TableView中单元格的背景颜色(在的cellForRowAtIndexPath)细胞背景颜色仅示出了对文本

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

    static NSString *CellIdentifier = @"Cell"; 

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


     cell.backgroundColor = [UIColor yellowColor]; 
     cell.contentView.backgroundColor = [UIColor yellowColor]; 

     UIView* backgroundView = [ [ [ UIView alloc ] initWithFrame:CGRectZero ] autorelease ]; 
     backgroundView.backgroundColor = [ UIColor yellowColor ]; 
     cell.backgroundView = backgroundView; 
     for (UIView* view in cell.contentView.subviews) 
     { 
      view.backgroundColor = [ UIColor yellowColor ]; 
     } 
    } 

    // Set up the cell... 
    if ([[menuList objectAtIndex:indexPath.row] isKindOfClass:[Category class]]) 
    { 
     cell.text = ((Category *)[menuList objectAtIndex:indexPath.row]).name; 
    } 
    else 
    { 
     [cell setText: [menuList objectAtIndex:indexPath.row]]; 
    } 


    return cell; 
} 

然而,当它呈现仅一部分单元格的背景应用类似于此(其中z是bg填充),即使辅助设备的背景是彩色的,但不是文本。

xxxxxxxxxxxxxxxxxxxx 
zzzSearch   
xxxxxxxxxxxxxxxxxxxx 

我看到能够改变文本的背景的唯一方法是通过设置整个表格视图的背景色在Interface Builder

我敢打赌,你的文字是不透明的UILabel。我记得不得不将myText.backgroundColor = [UIColor clearColor]添加到内容文本UILabel才能使其正常工作。

+0

感谢听起来像一个有前途的根到解决方案。我已经尝试了你的建议(请参阅上面的更新代码示例),但仍然没有快乐,有什么建议? – 2009-02-10 10:52:59

我已成功使用自定义TableViewCell对象做到这一点,但是我相信一定有办法做到这一点不