RFQuiltLayout 和 UICollectionView [英] RFQuiltLayout and UICollectionView

查看:11
本文介绍了RFQuiltLayout 和 UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 RFQuiltLayout 与我的 UICollectionView 一起使用.

I'm trying to use RFQuiltLayout with my UICollectionView.

我的 CollectionView 可以与标准 UICollectionViewFlowLayout 一起正常工作,但它只是一个网格.我希望我的照片布置得很好,如下所示这里

I have the CollectionView working fine with the standard UICollectionViewFlowLayout but it's just a grid. I want my photos to layout nicely as shown here.

我在理解将自定义布局与我的 UICollectionView 一起使用的要求时遇到了一些麻烦.

I'm having some trouble understanding what's required to use a custom layout with my UICollectionView.

我正在以编程方式做所有事情.我没有使用任何 Interface Builder/NIB/Storyboards.

I'm doing everything programatically. I'm not using any Interface Builder/NIBs/Storyboards.

让 UICollectionView 使用标准 FlowLayout 进行布局很容易,但是当我尝试更改为 RFQuiltLayout 时,我遇到了以下错误:

Getting the UICollectionView to layout with the standard FlowLayout was easy, but when I tried to change to the RFQuiltLayout I ran into the following error:

'UICollectionView must be initialized with a non-nil layout parameter'

这似乎是一个常见错误,但其他问题的建议/答案都没有帮助我解决它.

This seems to be a common error, but none of the suggestions/answers on the other questions helped me resolve it.

这是我的代码的相关部分:

So here's the relevant portions of my code:

.h

@interface PFRootViewController : UIViewController <OFFlickrAPIRequestDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic) UICollectionView *collectionView;

.m

//UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

//Used to use the above standard FlowLayout, but now trying to move to the below RFQuiltLayout

RFQuiltLayout* layout = (id)[self.collectionView collectionViewLayout];
layout.direction = UICollectionViewScrollDirectionVertical;
layout.blockPixels = CGSizeMake(100, 40);
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:layout];



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

[self.collectionView registerClass:[PFUICollectionViewCell class] forCellWithReuseIdentifier:@"FlickrCell"];


//Only Layout delegate method i've implemented:
- (CGSize) blockSizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 2 == 0)
        return CGSizeMake(80, 40);

    return CGSizeMake(40, 80);
}

我真的不知道我还需要做什么才能让它正常工作.如果我只是删除 RFQuiltLayout 引用并取消注释我的 UICollectionViewFlowLayout alloc init 一切正常,尽管使用标准 FlowLayout.

I really don't know what else I've got to do to get this working. If I simply remove the RFQuiltLayout references and uncomment my UICollectionViewFlowLayout alloc init everything works, albeit with the standard FlowLayout.

因此,如果有人可以帮助我指出正确的方向,那就太好了,这应该很简单,但是尝试开始工作确实很痛苦.- UICollectionViews 不像 UITableViews 那样简单.

So if anyone can help point me in the right direction that would be great, this should be simple but it's been a real pain to try and get working. - UICollectionViews aren't as simple as UITableViews.

昨天我花了很大一部分时间尝试让我的 UICollectionView 工作的各种自定义布局(包括 RFQuiltLayout)没有任何成功.

I spent a good portion of the day yesterday trying to get various custom layouts for my UICollectionView working (including RFQuiltLayout) without any success.

非常感谢任何帮助,

问候,约翰

推荐答案

RFQuiltLayout* layout = (id)[self.collectionView collectionViewLayout];

在这里,您在初始化集合视图之前的两行向集合视图询问布局对象.与您注释掉的行进行比较:

Here you're asking the collection view for a layout object, two lines before you initialise the collection view. Compare with the line you commented out:

//UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

你需要创建一个新的布局对象:

You need to create a new layout object:

RFQuiltLayout* layout = [[RFQuiltLayout alloc] init];

然后将其分配给您的当前视图.

Then assign it to your view as present.

查看存储库的自述文件,问题在于您正在从 viewDidLoad 方法复制代码,该方法假定集合视图及其布局已从情节提要中初始化.如果您在代码中创建视图,则还需要创建布局.

Looking at the README of the repository, the problem is that you are copying code from a viewDidLoad method, which assumes that the collection view and its layout have been initialised from a storyboard. If you're creating your view in code, you need to create the layout as well.

这篇关于RFQuiltLayout 和 UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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