我有一个字典数组,第一个键作为图像。我正在使用 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一起使用?
感谢您的帮助。
P.S。以下是错误消息。
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'
答案 0 :(得分:3)
您需要在标题中说明UIViewController
是UICollectionViewDataSource
和UICollectionViewDelegate
。 See this answer
答案 1 :(得分:2)
这是一个简单的例子。您的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;
}