xcode Swift 2 - 未定义 UICollectionViewFlowLayout 的行为,因为:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33437994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:08:41  来源:igfitidea点击:

Swift 2 - the behavior of the UICollectionViewFlowLayout is not defined because:

iosxcodeuicollectionviewswift2

提问by SNos

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

我正在使用 UICollectionrView 来显示视频。一切正常,只是我收到一条警告说:

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 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 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 test[6451:90574] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

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

2015-10-30 14:00:39.893 测试[6451:90574] 项目宽度必须小于 UICollectionView 的宽度减去部分插入左右值,减去内容插入左右值。

2015-10-30 14:00:39.893 test[6451:90574] 相关的 UICollectionViewFlowLayout 实例是 ,它附加到 ; 层 = ; 内容偏移量:{0, 0};contentSize: {320, 0}> 集合视图布局: .

2015-10-30 14:00:39.894 测试 [6451:90574] 在 UICollectionViewFlowLayoutBreakForInvalidSizes 处创建一个符号断点以在调试器中捕获它。

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

当我重新加载我的 collectionView 中的数据时,就会出现这个警告。

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

我试图改变宽度,但警告仍然存在。

What am I doing wrong?

我究竟做错了什么?

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
    }

回答by Erken

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.

重新加载数据后,您的元素之一似乎超过了 UICollectionView 的大小。我在从横向旋转到纵向时发生了此警告,其中我的横向单元格显然不适合纵向 UICollectionView。

Try invalidating the current layout before reloading your data:

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

self.collectionView.collectionViewLayout.invalidateLayout()

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.contentInsetto UIEdgeInsetsZero

如果这不起作用,请尝试将单元格的宽度设置为特定大小(例如 300)并从那里开始工作。您可能会发现边距或边缘占用了您意想不到的空间,这就是您的元素大于 UICollectionView 宽度的原因。如果是这样的话,尝试设置self.collectionView.contentInsetUIEdgeInsetsZero

回答by SammyRNYCreal

I'm still researching this error, "uicollectionviewflowlayout is not defined because." And I can't figure out the solution, yet (I will get you bug!), but I did narrow down exactly what is causing my issue, the contentOffset is being set with bad values and causing the error:

我仍在研究此错误,“未定义 uicollectionviewflowlayout 因为。” 我还想不出解决方案(我会让你犯错误!),但我确实缩小了导致我的问题的确切原因,contentOffset 设置的值不正确并导致错误:

contentOffset: {0, -64};

内容偏移:{0,-64};

I am using AKPickerView-Swift, I converted it to Swift 3. I only get the error when I embed the UIView, containing the AKPickerView, in a Navigation Controller (UINavigationController) on the storyboard.

我正在使用AKPickerView-Swift,我将它转换为 Swift 3。当我将包含 AKPickerView 的 UIView 嵌入故事板上的导航控制器 (UINavigationController) 时,我只会收到错误消息。

So the simple temporarysolution and just a work around, don't embed the UIView in a Navigation Controller. :) I'll come back later and correct this properly and figure out why UICollectionViewFlowLayout is being fired off for the Navigation Controller and not the UIViewController.

所以简单的临时解决方案和解决方法,不要将 UIView 嵌入到导航控制器中。:) 我稍后会回来并正确纠正这个问题,并弄清楚为什么 UICollectionViewFlowLayout 是为导航控制器而不是 UIViewController 触发的。

If anyone has free time to figure it out before I do, Thanks in Advance and it's greatly appreciated.

如果有人有空闲时间在我做之前弄清楚,提前致谢,非常感谢。

回答by Mary Martinez

I just got this error after I purged all builds and rebuilt the project. I had made several other changes and prior to this everything was working "correctly", or at least it appeared to be (perhaps due to the view being cached somehow - not sure).

在清除所有构建并重建项目后,我刚刚收到此错误。我进行了其他几项更改,在此之前一切都“正常”工作,或者至少看起来是(可能是由于视图以某种方式缓存 - 不确定)。

Anyway, I also had contentOffset: {0, -64} so I double checked the constraints and I found that my constraints were set on the superview (navigation controller), and not the view. I removed the constraints to the superview and re-added them, this time to the view. Now the contentOffset is {0, 0} and the error is gone.

无论如何,我也有 contentOffset: {0, -64} 所以我仔细检查了约束,我发现我的约束是在超级视图(导航控制器)上设置的,而不是视图。我删除了对超级视图的约束并重新添加了它们,这次是添加到视图中。现在 contentOffset 是 {0, 0} 并且错误消失了。

回答by Ram Madhavan

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let videoCell = collectionView.dequeueReusableCellWithReuseIdentifier("VideoCell", forIndexPath: indexPath) as UICollectionViewCell 
(........)
}

Is your UICollectionViewoutlet name collectionviewlike you specified here???

您的UICollectionView插座名称collectionview 是否像您在此处指定的那样???

If you are using a "UICollectionView" in the storyboard, then outlet name specified for that in the class should need to use here. Example:

如果您在情节提要中使用“UICollectionView”,则应在此处使用类中为其指定的插座名称。例子:

let videoCell = myCollectionViewOutletName.dequeueReusableCellWithReuseIdentifier("VideoCell", forIndexPath: indexPath) as MyCollectionViewCellClassName