2013-04-11 75 views
1

作为Xcode初学者,我想在我的应用程序的表格单元格中显示缩略图。现在,我有了解析JSON数据并将其发布到单元格的标题和副标题中的代码。如何在xCode中将缩略图图像显示到表单元格?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"]; 

    if(cell == nil){ 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"]; 
    } 

    cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"receta"]; 
    cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"koha"]; 

    return cell; 
} 

如何显示单元格右侧的缩略图?

谢谢。

+0

创建一个自定义单元,您可以使用您自己的布局。在Google上搜索自定义UITableViewCells。 – Popeye 2013-04-11 14:58:14

+0

Popeye,你会介意帮助我吗?我搜索了,但我没有找到对我有用的东西,因为我对Xcode有一些了解,这是我第三次今天打开它。 – 2013-04-11 15:00:27

+0

我会推荐一些教程比。这里是一个很好的教程http://blog.spritebandits.com/2012/03/13/creating-custom-uitableviewcells-from-xibs-step-by-step-tutorial/ – Popeye 2013-04-11 15:05:11

回答

3

如果您不想制作自定义单元格,您可以创建一个带有所需图像的UIImageView,并将其设置为单元格的accessoryView,该单元格将显示在单元格的右边缘。您还需要确保单元格的高度足够适合图像的高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 100; 
} 

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

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

    cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"some-image-name"]]; 

    return cell; 
} 

您可以返回一个常数(我选择100)如果图像将永远是一个固定的大小,也可以检查UIImagesize和返回类似size.height + 10一些额外的填充。

+0

我会倾向于说这不是什么'accessoryView'是为了,但它会完成这项工作。 +1用于出箱思考。 – Popeye 2013-04-12 07:06:33

+0

如果Amar不想制作自定义单元格,这是一种方法 – jszumski 2013-04-12 12:23:35

相关问题