0

最初のキーを画像として持つ辞書の配列があります。データソースとして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 がデータ ソースです。残念ながら、インターネットで見つけた多くのチュートリアルでは、データ ソースとして 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 の使い方がわかりません。では、UIViewController で UICollectionView を使用するにはどうすればよいでしょうか。

ご協力ありがとうございました。

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'
4

2 に答える 2

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;
 }
于 2013-03-20T06:16:21.713 に答える