2011-09-19 48 views
0

我確定這是一個問題,已被問及回答,但我似乎無法找到我需要的東西。我試圖自定義我的UITableView的背景,爲此,我創建了UITableViewCell的子類,並使用IB構建了我的自定義單元。我成功地將自定義單元格連接到表視圖,所以我得到的單元格正在顯示。我試圖改變每個單元格的背景,所以這裏是我使用:UITableViewCell自定義 - 更改背景

if(row == 1){ 
    rowBackground = [UIImage imageNamed:@"VehicleExpenses_button.png"]; 
    selectionBackground = [UIImage imageNamed:@"VehicleExpenses_button_selected.png"]; 
    ((UIImageView *)cell.backgroundView).image = rowBackground; 
    // ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground; 
} 

我的控制器類的

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

方法中。我用調試器檢查過了,代碼中的所有行都被調用...

任何想法?

這是整個方法:

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

    UIImage *rowBackground; 
    UIImage *selectionBackground; 

    NSInteger row = [indexPath row]; 


    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil]; 
     cell = (UITableViewCell *)[nib objectAtIndex:0]; 
    } 

    if(row == 1){ 

     rowBackground = [UIImage imageNamed:@"VehicleExpenses_button.png"]; 
     selectionBackground = [UIImage imageNamed:@"VehicleExpenses_button_selected.png"]; 
     ((UIImageView *)cell.backgroundView).image = rowBackground; 
     // ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground; 
    } 

    // perform additional custom work... 

    return cell; 
} 

回答

2

也許我錯了,但我沒有看到你在哪裏初始化backgroundView作爲UIImageView。以下是我想補充到您創建的細胞系:

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil]; 
     cell = (UITableViewCell *)[nib objectAtIndex:0]; 
     cell.backgroundView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.bounds.size.width, cellHeight)] autorelease]; 
     cell.selectedBackgroundView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.bounds.size.width, cellHeight)] autorelease]; 
    } 

注:與你的期望高度取代「cellHeight」。

我希望這會有所幫助。

+0

我試過上面這段代碼,它沒有工作......會有什麼可能的連接,我失蹤了? – coder

+0

我不確定這是否相關,但每次訪問tableView時,例如在tableView.bounds.size.width中,我都會收到一條警告:「本地聲明的'tableView'隱藏實例變量' – coder

+0

@Heather:這意味着你在你的.h文件「tableView」中調用了tableview對象。這是一個問題,因爲這個方法有一個局部變量,名字的確切名稱......編譯器不知道你指的是哪個。 – RyanM

0

您可以使用此:

UIImageView *cellBackGround = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"selectedRow.png"]]; 
[[tableView cellForRowAtIndexPath:indexPath] setBackgroundView:cellBackGround]; 
[cellBackGround release]; 
0

小問題。而不是鑄造和設置backgroundViewselectedBackgroundView的圖片屬性,將它們設置爲從您的圖片創建的UIImageView

cell.backgroundView = [[[UIImageView alloc] initWithImage:rowBackground] autorelease]; 
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:selectionBackground] autorelease]; 
+0

我剛試過這個,仍然沒有運氣。我應該改變視圖控制器中的單元格的背景,而不是自定義的UITableViewCell類,是否正確? – coder

+1

@ Heather:是的,更改控制器中單元格的背景。在你的例子中你試圖做的是正確的/流行的做法......查看我的答案;它可能會有幫助。 – RyanM

相關問題