2010-09-22 67 views
1

我有一些代碼可以改變在分組表格視圖中完美工作的單元的背景圖像。但是,並不是一張普通的表格。有誰知道爲什麼這些行爲有所不同?我想要做的就是給表格單元格添加一個背景圖像(普通)iPhone:普通表問題。沒有出現單元背景圖像

這對分組表格視圖完美。我是否需要自定義單元格才能使用普通視圖?

感謝西蒙

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 

UniversalAppAppDelegate *appDelegate = (UniversalAppAppDelegate *)[[UIApplication sharedApplication] delegate]; 

NSArray *rowData; 

// Check see if section Data is populated 
if (self.sectionData == nil) { 
    rowData = [(NSDictionary *)[tableData objectAtIndex:indexPath.section] objectForKey:@"SectionItems"]; 
} else { 
    rowData = [(NSDictionary *)[sectionData objectAtIndex:indexPath.section] objectForKey:@"SectionItems"]; //[self.sectionData objectAtIndex:indexPath.section]; 
} 

NSDictionary *cellDetails = [rowData objectAtIndex:indexPath.row]; 

// Clear any previous imageview 
UIView *view = [cell.backgroundView viewWithTag:99]; 
if (view != nil) 
    [view removeFromSuperview]; 

// Add a new imageview 
NSString *filename = [cellDetails objectForKey:@"TableItemFullImage"]; 
if (filename.length > 0) { 

    UIImageView *cellImageView = [[UIImageView alloc] initWithImage:[appDelegate imageForImageID:[cellDetails objectForKey:@"TableItemFullImage"] imageExtension:[cellDetails objectForKey:@"TableItemFullImageType"]]]; 
    cellImageView.tag = 99; 
    CGRect newFrame = cell.backgroundView.bounds; 
    cellImageView.frame = newFrame; 

    [cell.backgroundView addSubview:cellImageView]; 
    [cellImageView release]; 
} 

}

回答

2

你太過於複雜了。

UIImage * image = [appDelegate ...]; 
cell.backgroundView = [[[UIImageView alloc] initWithImage:image] autorelease]; 
+0

這麼簡單,完全工作 – Burf2000 2010-09-23 13:32:48

1

我懷疑,在平原表視圖,該indexPath.section將不會返回正確的數據。所以這幾行代碼可以給你一個nil rowData。仔細檢查是否

// Check see if section Data is populated 
if (self.sectionData == nil) { 
    rowData = [(NSDictionary *)[tableData objectAtIndex:indexPath.section] objectForKey:@"SectionItems"]; 
} else { 
    rowData = [(NSDictionary *)[sectionData objectAtIndex:indexPath.section] objectForKey:@"SectionItems"]; //[self.sectionData objectAtIndex:indexPath.section]; 
} 
+0

問題,我的其他開發人員說,你不能把一個圖像放在一個普通的單元格?他在說什麼BS – Burf2000 2010-09-22 15:02:11

+0

討論 普通樣式表(UITableViewStylePlain)中的單元格的默認值爲零,而分組樣式表UITableViewStyleGrouped的默認值爲零。 UITableViewCell將背景視圖添加爲所有其他視圖背後的子視圖並使用其當前幀位置。 http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html – vodkhang 2010-09-22 15:11:22

+0

上述評論來自UITableViewCell文檔。然後,我想,這是你的問題,你可能需要設置cell.backgroundView = cellImageView,而不是添加爲子視圖 – vodkhang 2010-09-22 15:12:38