2

I'm working on my first more complex app and couldn't find a solution to this one. I have loaded about 64 icons into my app. For a table view, a user can assign to each cell one of these icons. Basically when he edits the cell he gets to a new UIView with all the 64 icons. The currently chosen icon should have a border and if he clicks a different one, the border should move and the icon assigned to that selected item in the tableview.

My problems are now: a) How do I load these 64 icons into my view? I've created the view with all the UIImageViews, but how do I load them into these imageviews? Where are they saved if I copy them just to my directory and how do I access them? b) Is there an easier way than placing 64 different UIViews into this view manually and linking them up with IBOutles?

MichiZH
  • 5,587
  • 12
  • 41
  • 81

4 Answers4

2

Your problem could easily be resolved using UICollectionView . A UICollectionView just acts like a UITableView .

Go through the link in order to find more about UICollection

http://skeuo.com/uicollectionview-custom-layout-tutorial

Using UICollection view you just need the pass the image object saved in your Array

Gaurav Rastogi
  • 2,145
  • 1
  • 14
  • 19
  • But one of the serious downsides of this approach is the fact that your app will run only on iOS 6 and up and will not support earlier iOS versions. – Sergey Grischyov Apr 30 '13 at 14:27
0

You would have to do this in code. I would suggest you put all your filenames in NSArray, then iterate through it and with each step create a UIImageView on your ViewController programmatically. I suggest though that you subclass UIButton so it is "selected" when you tap it and you can then put any image into it.

Sergey Grischyov
  • 11,995
  • 20
  • 81
  • 120
0

That's a lot of questions on a wide variety of topics.

How do I load icons/images?

UIImage *image = [UIImage imageNamed:@"fubar.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

If you just copy images in your project they are part of the main bundle and for the most part you and pretty much ignore where they are saved. It's pretty easy to access them. (see above)

Is there an "easier way"... well, you'd have to give us a way if you want to know if there is an easier way. However I think a simple naming convention and a loop would be workable.

NSMutableArray *icons = [NSMutableArray array];
NSString *iconName;
UIImage *image;
UIImageView *imageView; = [[UIImageView alloc] initWithImage:image];
for (int count=0 ; count < 65 ; count++) {
    iconName = [NSString stringWithFormat:@"icon%d.png", count];
    image = [UIImage imageNamed:iconName];
    [icons addObject:image];        
}

Then you would use your icons array as the basis of your UITableView. About that though. You might want to look into using a UICollectionView instead. It's like a table, but it's a grid of items instead of just rows. It would lend itself better to a large set of images.

As for how you copy the images into the views, both your UITableView and your UICollectionView have methods where they "ask" for the data and give you a position. Based on the position information you just set the it asks for and it will handle the rest. It might look something like

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    // ...
    // remembering 'icons' is our array of image data
    cell.imageView.image = icons[indexPath.row]; 
    // ...
}

Edit from Comments: How to tell if a file exists in the app bundle? (my answer is just a cut/paste)

[[NSFileManager defaultManager] fileExistsAtPath:pathAndFileName];
Community
  • 1
  • 1
DBD
  • 23,075
  • 12
  • 60
  • 84
  • One follow up question though: How can I check if a certain image is stored in the app bundle or not? – MichiZH May 01 '13 at 08:27
0

you can use gridView a open source component here is the link

GridView

Also to detect you can assign tag to each imageView like from 0 to 63 then when user choose 0th image you can assign that tag to a local variable so that next time when user views grid you can identify that user has previously chose 0th index image. I hope it helps you.

Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256