2011-06-10 141 views
0

這個問題很簡單,但很煩人。我在視圖控制器中放置了一個分組樣式的UITableView,並使控制器成爲表視圖的委託和數據源。自定義UITableView背景顏色

當我什麼改變表格的背景顏色,我用:

UIColor *backColor = [UIColor colorWithRed:(15.0/255.0) green:(170.0/255.0) blue:(230.0/255.0) alpha: 0.75f]; 

[self.myTable setBackgroundColor:backColor]; 

tableView:tableView cellForRowAtIndexPath:indexPath方法,我只返回一個簡單的細胞:

static NSString *CellIdentifier = @"CellIdentifier"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
if (cell == nil){ 
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:moreCellIdentifier] autorelease]; 
} 

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
return cell; 

但奇怪事情是在每個單元格的圓角處顏色是不正確的,我認爲這是因爲單元格的其中一個子視圖的框架大於單元格,我試圖使子視圖的框架很小但仍然失敗任何想法?謝謝。

+0

我認爲這是單元格寬度的問題,我將UITableViewCell分類並使寬度僅爲200的新單元類,但在表格視圖中單元格仍佔用幾乎整個屏幕寬度。 – joe522 2011-06-10 17:40:53

回答

0

請嘗試在您的tableView:tableView cellForRowAtIndexPath:indexPath函數中使用下面的代碼。

NSString *CellIdentifier = [[NSString alloc] initWithFormat:@"CellIdentifier_%d_%d",indexPath.section,indexPath.row]; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
    if (cell == nil){ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
    reuseIdentifier:moreCellIdentifier] autorelease]; 
    } 
    [CellIdentifier release]; 
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
    return cell; 
+0

這些代碼只是改變了爲細胞分配細胞標識符的方式,它與我在這裏遇到的問題無關。 – joe522 2011-06-10 17:15:50

+0

@ joe522:你試過了嗎? – Jhaliya 2011-06-10 17:17:10

+0

@Jhaliya:是的,它不起作用。我發現這是單元格寬度的問題,我將UITableViewCell分類並使寬度僅爲200的新單元類,但在表格視圖中,單元仍佔用幾乎整個屏幕寬度。 – joe522 2011-06-10 17:40:06

0

看你的代碼,這是我不明白這行:

[self.myTable setBackgroundColor:backColor]; 

指。什麼是myTable?什麼是自我?如果你繼承UITableViewController,最好的地方在哪裏執行的代碼是viewWillAppear

UIColor *backColor = [UIColor colorWithRed:(15.0/255.0) green:(170.0/255.0) blue:(230.0/255.0) alpha: 0.75f]; 
tableViewController.tableView.backgroundColor = backColor; 

(使用:

無論如何,如果你使用的是UITableViewController來管理你的表,你應該改變背景顏色這樣的自己而不是tableViewController.tableView.backgroundColor)。