Ios 以UIViewController作为数据源的UICollectionView

Ios 以UIViewController作为数据源的UICollectionView,ios,uiviewcontroller,uicollectionview,Ios,Uiviewcontroller,Uicollectionview,我有一个字典数组,第一个键是图像。我正在使用UIViewController作为数据源 @interface myViewController () { NSMutableArray *textureArray1; NSMutableDictionary *dict1; UIImage *key1a; // UIImage NSString *key1b; // texture name } 使用UIViewController中的TableView控件填充此字

我有一个字典数组,第一个键是图像。我正在使用UIViewController作为数据源

@interface myViewController () {
    NSMutableArray *textureArray1;
    NSMutableDictionary *dict1;
    UIImage *key1a; // UIImage
    NSString *key1b; // texture name
}
使用UIViewController中的TableView控件填充此字典数组没有问题

- (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;
}
我想做些新的事情。我想用UICollectionView填充相同的图像数组,其中UIViewController是数据源。不幸的是,我在Internet上找到的许多教程都将UICollectionViewController作为数据源。无论如何,我有以下几行代码

-(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;
}
应用程序崩溃时会出现一个错误,上面写着“无法识别的选择器发送到实例…”我不知道如何使用UICollectionView。那么,如何将UICollectionView与UIViewController结合使用呢

谢谢你的帮助

请注意,以下是错误消息

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'

您需要说明您的
UIViewController
是标题中的
UICollectionViewDataSource
UICollectionViewDelegate

您需要说明您的
UIViewController
是标题中的
UICollectionViewDataSource
UICollectionViewDelegate

这是一个简单的例子。您的viewController应符合
UICollectionViewDelegate
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;
 }

这是一个简单的例子。您的viewController应符合
UICollectionViewDelegate
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;
 }

发布实际的错误消息。确定。我已经编辑了原始问题。我认为错误是由于您从[[textureArray1 objectAtIndex:indexPath.row]objectForKey:key1b]返回的内容造成的。您应该记录从该表达式返回的内容的类。它似乎是一个字符串,而不是一个图像。。。这是我自己的愚蠢错误。有个打字错误。cell.backgroundColor=[UIColor colorWithPatternImage:[[textureArray1 objectAtIndex:indexPath.row]objectForKey:key1a]];对不起,伙计们。谢谢你抽出时间。是的,勒马尔。谢谢。发布实际的错误消息。当然。我已经编辑了原始问题。我认为错误是由于您从[[textureArray1 objectAtIndex:indexPath.row]objectForKey:key1b]返回的内容造成的。您应该记录从该表达式返回的内容的类。它似乎是一个字符串,而不是一个图像。。。这是我自己的愚蠢错误。有个打字错误。cell.backgroundColor=[UIColor colorWithPatternImage:[[textureArray1 objectAtIndex:indexPath.row]objectForKey:key1a]];对不起,伙计们。谢谢你抽出时间。是的,勒马尔。谢谢,好的。谢谢你的建议,好的。谢谢你的建议。