2010-02-08 31 views
1

我是iphone開發的新手。我想在每個單元格中顯示圖像。我只有圖像的一個數組的URL。請幫助我。謝謝。如何在單元格中顯示圖像?

+2

每個單元?什麼細胞?你有什麼嘗試? –

+0

在表格視圖單元格中?請澄清您的問題並添加代碼以指明您嘗試過的內容。 –

回答

4
//put this code in cellForRowAtIndexPath.... I'm assuming you have an array of url's that point to images 
id urlPath = [yourArray objectAtIndex:indexOfImage]; 
NSURL *url = [NSURL URLWithString:urlPath]; 
NSData *data = [NSData dataWithContentsOfURL:url]; 
UIImage *image = [UIImage imageWithData:data]; 


static NSString *CellIdentifier = @"Cell"; 

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

//insert an image into your cell 
cell.imageView = image; 

有可能是一個更有效的方法來做到這一點,但它是一個體面的方式來開始自定義你的tableview單元格。 ImageView是UITableViewCell類的許多屬性之一。

延伸閱讀.... UITableViewCell Class Reference

+2

這是可行的,但最好不要從主線程上的網絡加載圖像(如果從磁盤上它一般可以) – rpetrich

+0

感謝它工作正常...... – Warrior

2

你應該看看這個蘋果的示例代碼,它是偉大的,顯示瞭如何實現你所需要的以及其他有用的東西: Advanced Table View Cells

+0

我有圖像的字符串的url數組中,上面的示例具有資源中的所有圖像。 – Warrior

相關問題