2012-01-31 39 views
1

有人可以告訴我爲什麼這不工作正確嗎?UIView背景顏色不生效

我的表格視圖單元格中有以下代碼行,用於tableView:didSelectAtIndexRowPath:方法。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [[tableView viewWithTag:199]removeFromSuperview]; 

    CGSize cellSize = [tableView cellForRowAtIndexPath:indexPath].frame.size; 

    UIView *subSelectionView = [[UIView alloc]initWithFrame:CGRectMake(10, 0, (int)cellSize.width - 20, 100)]; 

    [subSelectionView setBackgroundColor:[UIColor blueColor]]; 

    subSelectionView.layer.borderColor = [UIColor grayColor].CGColor; 
    subSelectionView.layer.borderWidth = 1; 

    subSelectionView.tag = 199;  

    [[tableView cellForRowAtIndexPath:indexPath]addSubview:subSelectionView]; 
} 

注意代碼:

[subSelectionView setBackgroundColor:[UIColor blueColor]]; 

很明顯,我想改變,我加入到UITableViewCell子視圖的顏色,而是它爲什麼不工作?

+0

確保'subSelectionView'不被表格視圖單元格的內容視圖覆蓋,因爲這似乎是發生了什麼。 – Costique 2012-01-31 05:10:04

回答

2

強制重裝該表添加子視圖在桌子上後

[[tableView cellForRowAtIndexPath:indexPath]addSubview:subSelectionView]; 

[tableview reloadData]; 
+0

謝謝..它適用於我..:D很高興你回答了兩個.. – Aldee 2012-01-31 05:13:36

1

以下行添加到方法的開始

[tableView deselectRowAtIndexPath:indexPath animated:NO]; 

所以烏爾法將

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 

    [[tableView viewWithTag:199]removeFromSuperview]; 
    .... 
}