2009-02-10 93 views
0

我已經作爲走得更遠,使用以下代碼來設置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

回答

0

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

+0

感謝聽起來像一個有前途的根到解決方案。我已經嘗試了你的建議(請參閱上面的更新代碼示例),但仍然沒有快樂,有什麼建議? – 2009-02-10 10:52:59

0

我已成功使用自定義TableViewCell對象做到這一點,但是我相信一定有辦法做到這一點不