Swift 2-未定义UICollectionViewFlowLayout的行为,因为: [英] Swift 2 - the behavior of the UICollectionViewFlowLayout is not defined because:

查看:418
本文介绍了Swift 2-未定义UICollectionViewFlowLayout的行为,因为:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UICollectionrView来显示视频.一切正常,除了我得到警告:

I am using a UICollectionrView to show videos. Everything works fine except that I get a warning saying :

2015-10-30 14:00:39.893测试[6451:90574]的行为 未定义UICollectionViewFlowLayout,因为:

2015-10-30 14:00:39.893 test[6451:90574] the behavior of the UICollectionViewFlowLayout is not defined because:

2015-10-30 14:00:39.893测试[6451:90574]商品宽度必须小于 大于UICollectionView的宽度减去该部分的插入量 和右值,减去内容插入左和右值.

2015-10-30 14:00:39.893 test[6451:90574] the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.

2015-10-30 14:00:39.893测试[6451:90574]相关 UICollectionViewFlowLayout实例为,并且已附加到;层=; contentOffset:{0,0}; contentSize:{320,0}>集合 视图布局:.

2015-10-30 14:00:39.893 test[6451:90574] The relevant UICollectionViewFlowLayout instance is , and it is attached to ; layer = ; contentOffset: {0, 0}; contentSize: {320, 0}> collection view layout: .

2015-10-30 14:00:39.894测试[6451:90574]在 UICollectionViewFlowLayoutBreakForInvalidSizes可以在 调试器.

2015-10-30 14:00:39.894 test[6451:90574] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

仅当我在collectionView中重新加载数据时,才会出现此警告.

This warning appears just when I reload the data inside my collectionView.

我试图更改宽度,但警告仍然存在.

I have tried to change width but the warning stills there.

我在做什么错了?

var UserVideosInfo = [[String]]()

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: "reloadCollectionVideoView:",
            name: "ReloadCollectionVideoView",
            object: nil)   
    }

@objc func reloadCollectionVideoView(notification: NSNotification) {
        UserVideosInfo = NSUserDefaults.standardUserDefaults().objectForKey("UserVideosJSON") as! [[String]]
        print(UserVideosInfo)
        collectionView?.reloadData()

    }

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        if UserVideosInfo.count == 0 {
            return 0
        }else{
        return UserVideosInfo.count
        }

    }



    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let videoCell = collectionView.dequeueReusableCellWithReuseIdentifier("VideoCell", forIndexPath: indexPath) as UICollectionViewCell
        let communityViewController = storyboard?.instantiateViewControllerWithIdentifier("community_id")

        videoCell.frame.size.width = (communityViewController?.view.frame.size.width)!
        videoCell.center.x = (communityViewController?.view.center.x)!

        return videoCell
    }

推荐答案

重新加载数据后,您的其中一个元素看起来超过了UICollectionView的大小.我已经在从横向旋转到纵向旋转时发生了此警告,我的横向单元格显然不适合放置在纵向UICollectionView中.

Looks like one of your elements exceeds the size of your UICollectionView after you reload your data. I've had this warning happen on rotation from landscape to portrait, where my landscape cells would obviously not fit inside a portrait UICollectionView.

在重新加载数据之前尝试使当前布局无效:

Try invalidating the current layout before reloading your data:

self.collectionView.collectionViewLayout.invalidateLayout()

如果这不起作用,请尝试将单元格的宽度设置为特定大小(例如300),然后从那里开始工作.您可能会发现有空白或边缘占用了您不期望的空间,这就是为什么您的元素大于UICollectionView的宽度的原因. 如果是这种情况,请尝试将self.collectionView.contentInset设置为UIEdgeInsetsZero

If that doesn't work, try setting the width of your cells to a specific size (for example 300) and work from there. You may find that there are margins or edges that take space that you weren't expecting, and that's why your elements are larger than the width of your UICollectionView. If that is the case, try setting the self.collectionView.contentInset to UIEdgeInsetsZero

这篇关于Swift 2-未定义UICollectionViewFlowLayout的行为,因为:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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