UIVollectionView与UIViewController作为数据源

我有一个第一个键作为图像的字典数组。 我使用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上find的许多教程都将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; } 

和应用程序崩溃,说:“无法识别的select器发送到实例…”呃,我不知道如何使用UICollectionView。 那么你如何使用UIVolControlView与UIViewController?

感谢您的帮助。

PS以下是错误信息。

 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是一个UICollectionViewDataSourceUICollectionViewDelegate在你的头部。 看到这个答案

这是一个简单的例子。 你的viewController应该符合UICollectionViewDelegateUICollectionViewDelegateFlowLayout

 - (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; }