向表视图中的每个单元格添加不同的图标

问题描述 投票:3回答:3

我想在应用程序中的表格视图中添加不同的图标。任何人都可以通过适当的数组引导我,或者在可能的情况下显示一些示例代码或示例。将感谢您的帮助。

谢谢,克里斯蒂

iphone cocoa-touch xcode ipad uitableview
3个回答
11
投票

说图标在应用程序包中存储为icon0.png,icon1.png等。您可以将每个单元格的图标设置为

cell.imageView.image = [UIImage imageNamed:    
              [NSString stringWithFormat:@"icon%d.png",indexPath.row]];

Swift

    cell.imageView?.image = UIImage(named: "icon\(indexPath.row).png")

3
投票

嗯,您在某个地方有一个数组可以在numberOfRowsInSection中返回正确的数字。该数组是您用来填充单元格的数组。如果只想在单元格中显示图标,则图标数组就是这样。

因此,例如:

UIImage *one = ...;
UIImage *two = ...;
[arrayIcons addObject: one];
[arrayIcons addObject: two];

并且在numberOfRowsInSection中,返回[arrayIcons count]

在方法cellForRowAtIndexPath中,具有变量indexPath。如果您想知道您拥有哪个单元格,请使用:indexPath.row

因此,如果您加载具有UIImageView的单元格(可能是自定义的,请参见其他答案)(例如,它的名称为:imageView,然后像这样将图标加载到单元格中:

cell.imageView = [UIImage imageWithContentsOfFile:[arrayIcons objectAtIndex:indexPath.row]];

1
投票

您必须为UITableView定义自定义单元格。

This tutorial是您要寻找的。

© www.soinside.com 2019 - 2024. All rights reserved.