2009-04-19 42 views
2

我正在嘗試獲取UITableView滾動的UIView(如果啓用了滾動)。通常情況下,背景是白色的,如果您將UITableView從邊界中移出,則會看到背景。我試圖將此背景設置爲blackColor的UIColor。我似乎無法找到合適的標籤。我在我的UIViewController中試過以下代碼:找到UITableView滾動的UIView

- (void)loadView 
{ 
    [super loadView]; 
    UITableView *aTableView = 
     [[[UITableView alloc] 
      initWithFrame:self.view.bounds 
      style:UITableViewStylePlain] autorelease]; 

    [aTableView setScrollEnabled:YES]; 
    [self.view setBackgroundColor:[UIColor blackColor]]; 
    [self.view addSubview:aTableView]; 
    self.tableView = aTableView; 
} 

顏色仍然保持白色。似乎我打錯了UIView。

有什麼想法?謝謝。

回答

0

我沒有解決方案,但我確實有一個建議。你有沒有嘗試使UITableView的backgroundColor透明並將超級視圖的顏色設置爲白色?

+0

嘿羅傑,我不想讓blackColor滲入我的UITableView。我想要它的正常樣式。我只想看看錶格滾動出界時的背景。 – Coocoo4Cocoa 2009-04-20 15:30:06

0

我是能夠實現你想要什麼,但它看起來像一個解決方法,我(也許有對庫中的錯誤)。

首先,您需要在UITableView中將backgroundColor屬性設置爲黑色。

然後,當你創建一個新的UITableViewCell,你需要做這樣的事情(此代碼的那張UITableView中的數據源):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *MyIdentifier = @"MyIdentifier"; 

    // Try to retrieve from the table view a now-unused cell with the given identifier 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    // If no cell is available, create a new one using the given identifier 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
    } 
    cell.contentView.backgroundColor = [UIColor whiteColor]; 

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 60, 20)] autorelease]; 
    label.text = @"test"; 
    label.textColor = [UIColor blackColor]; 
    [cell.contentView addSubview:label]; 
    cell.textColor = [UIColor blackColor]; 

    return cell; 
} 

你必須設置的內容查看到白色的背景顏色,但當你這樣做時,單元格的文本停止出現,所以你需要添加一個UILabel到它的內容視圖並自己寫文本。

這似乎矯枉過正只是改變背景顏色,但可能有一種防止設置上的UITableViewCell的不是其包含的UITableView不同的背景顏色的錯誤。

0

嗯..我在一個類似的問題設定cellForRowAtIndexPath方法中的細胞的背景顏色運行。 解決方法是在回調方法 willDisplayCell中設置背景顏色。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell.backgroundColor = (indexPath.row % 2 == 0) ? DARK_BACKGROUND : LIGHT_BACKGROUND; 
}