Vipin
Vipin

Reputation: 4728

Adding different icons to each cells in table view

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

Upvotes: 3

Views: 7977

Answers (3)

ArunGJ
ArunGJ

Reputation: 2683

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")

Upvotes: 12

Joetjah
Joetjah

Reputation: 6132

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]];

Upvotes: 3

sergio
sergio

Reputation: 69027

You wil have to define custom cells for your UITableView.

This tutorial is what you are looking for.

Upvotes: 1

Related Questions