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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6136352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:07:40  来源:igfitidea点击:

Adding different icons to each cells in table view

iphonecocoa-touchxcodeipaduitableview

提问by Christina

I want to add different icons to the table view present in my app.Can anyone guide me through a proper array or if possible show some sample code or example.Any help will be appreciated.

我想在我的应用程序中的表格视图中添加不同的图标。任何人都可以指导我完成正确的数组,或者如果可能的话显示一些示例代码或示例。任何帮助将不胜感激。

Thanks, Christy

谢谢,克里斯蒂

回答by ArunGJ

Say the icons are stored as icon0.png, icon1.png and so on in the app bundle. You can give the icon for each cell as

假设图标在应用程序包中存储为 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")

回答by Joetjah

Well, you have an array somewhere to return the correct number in numberOfRowsInSection. That array is the one you use to fill up the cells. If you only want to display icons in your cells, your array of icons is like that.

好吧,您在某处有一个数组可以在numberOfRowsInSection. 该数组是您用来填充单元格的数组。如果您只想在单元格中显示图标,那么您的图标数组就是这样。

So, for example:

因此,例如:

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

and in numberOfRowsInSection, return [arrayIcons count].

并在 numberOfRowsInSection 中返回[arrayIcons count]

In the method cellForRowAtIndexPath, you have the variable indexPath. If you want to know which cell you have, you use: indexPath.row.

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

So, if you load the cell (probably custom, see other answers), which has a UIImageView(say it's named: imageView), and you load the icons in the cells like this:

因此,如果您加载单元格(可能是自定义的,请参阅其他答案),它有一个UIImageView(比如它的名称:imageView),并且您在单元格中加载图标,如下所示:

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

回答by sergio

You wil have to define custom cells for your UITableView.

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

This tutorialis what you are looking for.

本教程正是您要找的。