【问题标题】:UICollectionView Not AppearingUICollectionView 没有出现
【发布时间】:2012-09-21 08:04:09
【问题描述】:

我正在尝试在扩展UIViewController 的视图控制器中设置UICollectionView 以编程方式。出于某种原因,我的收藏视图根本没有出现。以下是我所拥有的。

为什么没有出现?我将它连接到委托和数据源,并将其作为子视图添加到self.view。我的代码中缺少什么?

在我的.h 文件中:

@interface MainViewController : UIViewController
{
    @private
    UICollectionView *_collectionView;
    NSMutableArray *_results; // data source array
}
@end

在我的.m 文件中:

@interface MainViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *results;
@end

@implementation MainViewController

@synthesize collectionView = _collectionView;
@synthesize results = _results;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // some init stuff - nothing to do with collection view.
    }

    return self;
}

- (void)loadView
{
    self.results = [NSMutableArray array];
    UIImage *image1 = [UIImage imageNamed:@"img1.jpg"];
    UIImage *image2 = [UIImage imageNamed:@"img2.jpg"];
    [self.results addObject:image1];
    [self.results addObject:image2];

    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    self.collectionView = collectionView;

    [self.view addSubview:self.collectionView];

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    [self.collectionView reloadData];

}

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
    return [self.results count];
}

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithPatternImage:[self.results objectAtIndex:indexPath.row]];
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    UIImage *image = [self.results objectAtIndex:indexPath.row];    
    return CGSizeMake(image.size.width, image.size.height);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(50, 20, 50, 20);
}

【问题讨论】:

    标签: objective-c ios xcode ipad uicollectionview


    【解决方案1】:

    除非我将 loadView 方法更改为 viewDidLoad,否则我在尝试运行您的代码时遇到错误——根据您不应该直接调用 loadView 的文档。为了让数据源和委托方法运行,我将设置委托和数据源的行移到了 self 下面设置 self.collectionView = collectionView

        self.collectionView = collectionView;
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
    

    【讨论】:

    • 那行不通。我最终继承了UICollectionViewController 并将init 方法更改为符合UICollectionViewController 的方法并且它起作用了。我遇到了cellForIndexPath 的问题 - 我会为此发布一个新帖子。
    • 我不知道为什么它对你不起作用——我刚刚制作了一个新应用程序,输入你的代码,并进行了我提到的 2 项更改,它运行良好。
    • 这很奇怪。您也可以显示项目吗?我的cellForItemInIndexPath 有问题。它给了我一个could not dequeue 错误。你根本不使用 xib/storyboard 吗?
    • 没有。我所做的唯一事情就是从单一视图模板创建一个新应用程序,并将您的代码放入默认的 ViewController.h 和 .m 中。我将 MainViewController 的任何提及更改为 ViewController,拖入两个图像,并进行了我在回答中提到的 2 个更改。我在屏幕左上角看到 2 张图片,没有错误消息。
    • 我也做了同样的事情,它奏效了。有趣的。你也可以上下滚动吗?我多次添加相同的图像,但看不到滚动。没有允许/禁止 UICollectionView 滚动的标志。
    【解决方案2】:

    您的numberOfSectionsInCollectionView: 返回0。对于包含一个部分的集合视图,您应该返回 1 或不实现此方法。

    我也看不到你在哪里分配/初始化self.collectionView

    【讨论】:

    • 我刚刚将部分返回更改为 0。请参阅上面的更新。我将UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout]; self.collectionView = collectionView; 添加到loadView,现在我得到的只是一个空白的黑屏。当我更改 collectionView 的背景时,屏幕会改变颜色,并且我知道 collection view 正在显示。它没有调用任何数据源或委托方法,因此根本不会添加数据。
    • @Darksky:尝试将设置从loadView 移动到viewDidLoad。我现在无法自己测试它,但这是设置视图的自定义元素的常用位置。
    【解决方案3】:

    我最终继承了 UICollectionViewController 而不是 UIViewController 并将 init 方法更改为:

    - (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout

    它成功了。

    【讨论】:

      【解决方案4】:

      您只需将 CollectionView 委托和数据源绑定到 StoryBoard 中的 ViewController。enter image description here

      【讨论】:

      • 以编程方式描述如何做到这一点。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-23
      相关资源
      最近更新 更多