如何在单元格中显示图像?

时间:2010-02-08 20:49:37

标签: iphone objective-c image cell

我是iphone开发的新手。我想在每个单元格中显示图像。我只有一个数组中的图像的网址。请帮助我。谢谢。

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

答案 1 :(得分:2)

你应该看看这个苹果的示例代码,这很棒,并展示了如何实现你需要的东西以及其他有用的东西: Advanced Table View Cells