使用UIViewController作为数据源的UICollectionView [英] UICollectionView with UIViewController As Data Source

查看:98
本文介绍了使用UIViewController作为数据源的UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
}

我在填充此字典数组时遇到问题在UIViewController中布局的 TableView 控件。

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

我想做点新事。我想用 UICollectionView 填充相同的图像数组,其中,UIViewController也是数据源。不幸的是,我在Internet上找到的许多教程都有UICollectionViewController作为数据源。无论如何,我有以下几行代码。

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

应用程序崩溃时出现无法识别的选择器发送到实例的错误。 ..好吧,我不知道如何使用UICollectionView。那么如何使用UICollectionView和UIViewController?

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?

感谢您的帮助。

P.S。以下是错误消息。

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'


推荐答案

你需要说你的 UIViewController 是你的 UICollectionViewDataSource UICollectionViewDelegate 头。 查看此答案

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

这篇关于使用UIViewController作为数据源的UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆