I want to add different icons to the table view present in my app.Can anyone guide me through a prope开发者_如何学Cr array or if possible show some sample code or example.Any help will be appreciated.
Thanks, Christy
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
cell.imageView.image = [UIImage imageNamed:
[NSString stringWithFormat:@"icon%d.png",indexPath.row]];
Swift
cell.imageView?.image = UIImage(named: "icon\(indexPath.row).png")
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.
So, for example:
UIImage *one = ...;
UIImage *two = ...;
[arrayIcons addObject: one];
[arrayIcons addObject: two];
and in numberOfRowsInSection, return [arrayIcons count]
.
In the method cellForRowAtIndexPath
, you have the variable indexPath
. If you want to know which cell you have, you use: 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:
cell.imageView = [UIImage imageWithContentsOfFile:[arrayIcons objectAtIndex:indexPath.row]];
You wil have to define custom cells for your UITableView.
This tutorial is what you are looking for.
精彩评论