0

I have an array of dictionary with the first key as an image. I'm using UIViewController as the data source.

@interface myViewController () {
    NSMutableArray *textureArray1;
    NSMutableDictionary *dict1;
    UIImage *key1a; // UIImage
    NSString *key1b; // texture name
}

I don't have trouble populating this dictionary array with a TableView control laid out in the UIViewController.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    UIImage *image = [[textureArray1 objectAtIndex:indexPath.row] objectForKey:key1a];
    cell.textLabel.text = [[textureArray1 objectAtIndex:indexPath.row] objectForKey:key1b];
    cell.imageView.image = image;
    return cell;
}

I want to do something new. And I want to populate the same array of images with UICollectionView where, again, UIViewController is the data source. Unfortunately, many tutorials that I find on the Internet have UICollectionViewController as the data source. Anyway, I have the following lines of code.

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithPatternImage:[[textureArray1 objectAtIndex:indexPath.row] objectForKey:key1b]];
    return cell;
}

And the application crashes with an error that says "unrecognized selector sent to instance..." Well, I don't know how to use UICollectionView. So how do you use UICollectionView with UIViewController?

Thank you for your help.

P.S. The following is the error message.

2013-03-20 10:21:07.777 iPadapp[2023:c07] -[__NSCFString _tiledPatternColor]: unrecognized selector sent to instance 0xdeacdf0
2013-03-20 10:21:07.778 iPadapp[2023:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString _tiledPatternColor]: unrecognized selector sent to instance 0xdeacdf0'
El Tomato
  • 6,479
  • 6
  • 46
  • 75
  • Post the actual error message. – rdelmar Mar 20 '13 at 06:06
  • Sure. I've edited the original question. – El Tomato Mar 20 '13 at 06:24
  • 1
    I think the error is due to what you're returning from [[textureArray1 objectAtIndex:indexPath.row] objectForKey:key1b]]. You should log the class of what you get back from that expression. It seems to be a string, not an image. – rdelmar Mar 20 '13 at 06:30
  • Oops... It was my own silly mistake. There was a typo. cell.backgroundColor = [UIColor colorWithPatternImage:[[textureArray1 objectAtIndex:indexPath.row] objectForKey:key1a]]; I'm sorry, guys. And thank you for your time. – El Tomato Mar 20 '13 at 06:37

2 Answers2

3

You need to say that your UIViewController is a UICollectionViewDataSource and UICollectionViewDelegate in your header. See this answer

Community
  • 1
  • 1
Ric
  • 8,615
  • 3
  • 17
  • 21
2

This is a simple example. Your viewController should be conformed to UICollectionViewDelegate and UICollectionViewDelegateFlowLayout

- (void)viewDidLoad
{
   [super viewDidLoad];

    // Create the collection view and add it as a sub view 
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

layout.itemSize = CGSizeMake(120, 120);
layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
layout.minimumInteritemSpacing =10.0f;
layout.minimumLineSpacing = 10.0f;

    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;

[self.collectionView registerClass:[UICollectionViewCell  class] forCellWithReuseIdentifier:@"cell"];

[self.view addSubview:self.collectionView];
}

 // Implement the required data source methods
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
 {
  return 50;
 }


   - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

  {
     static NSString *cellId =@"cell";
     CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];

     cell.backgroundColor =  [UIColor redColor]; 
  return cell;
 }
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110