滚动其中具有Collectionview的TableView时崩溃 [英] crash while scrolling through the TableView which haves a Collectionview in it

查看:77
本文介绍了滚动其中具有Collectionview的TableView时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个问题( UITableViewCell内的UICollectionView-AutoLayout )

,并尝试了几种互联网上可用的解决方案后,我附带了这个 @Pablo罗密欧的答案我尝试了他的解决方案,现在我得到了一个带有collectionView单元格的TableView(它可以根据其中的内容动态更改其大小) 但是当在我的tableview中滚动时,它因以下错误而崩溃:

    2016-06-13 22:05:51.546 WishMeluck[3507:425240] the behavior of the UICollectionViewFlowLayout is not defined because:
2016-06-13 22:05:51.546 WishMeluck[3507:425240] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
2016-06-13 22:05:51.546 WishMeluck[3507:425240] Please check the values return by the delegate.
2016-06-13 22:05:51.547 WishMeluck[3507:425240] The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fb83c05de90>, and it is attached to <UICollectionView: 0x7fb83a0c7c00; frame = (0 82.5; 375 179); clipsToBounds = YES; autoresize = RM+BM; tag = 4; gestureRecognizers = <NSArray: 0x7fb83c05cf00>; layer = <CALayer: 0x7fb83c055fb0>; contentOffset: {0, 0}; contentSize: {539, 179}> collection view layout: <UICollectionViewFlowLayout: 0x7fb83c05de90>.
2016-06-13 22:05:51.547 WishMeluck[3507:425240] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
2016-06-13 22:05:51.548 WishMeluck[3507:425240] the behavior of the UICollectionViewFlowLayout is not defined because:
2016-06-13 22:05:51.548 WishMeluck[3507:425240] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
2016-06-13 22:05:51.548 WishMeluck[3507:425240] Please check the values return by the delegate.
2016-06-13 22:05:51.548 WishMeluck[3507:425240] The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fb83c05de90>, and it is attached to <UICollectionView: 0x7fb83a0c7c00; frame = (0 82.5; 375 179); clipsToBounds = YES; autoresize = RM+BM; tag = 4; gestureRecognizers = <NSArray: 0x7fb83c05cf00>; layer = <CALayer: 0x7fb83c055fb0>; contentOffset: {0, 0}; contentSize: {539, 179}> collection view layout: <UICollectionViewFlowLayout: 0x7fb83c05de90>.
2016-06-13 22:05:51.548 WishMeluck[3507:425240] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
(lldb) 

如果有人知道我无法找出此错误的原因,请帮帮我

将此代码添加到我的TableViewCell

        override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

//        collectionViewContainer.frame = self.bounds;
//        collectionViewContainer.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

        let flow: UICollectionViewFlowLayout = (self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout)
        // Configure the collectionView
        flow.minimumInteritemSpacing = 1
            // This enables the magic of auto layout.
            // Setting estimatedItemSize different to CGSizeZero
            // on flow Layout enables auto layout for collectionView cells.
            // https://developer.apple.com/videos/play/wwdc2014-226/
            flow.estimatedItemSize = CGSizeMake(1, 1)
        // Disable the scroll on your collection view
        // to avoid running into multiple scroll issues.
        self.collectionView.scrollEnabled = true
    }

    func bindWithModel(model: AnyObject) {
        // Do your stuff here to configure the tableViewCell
        // Tell the cell to redraw its contentView



        self.contentView.layoutIfNeeded()
    }

    // THIS IS THE MOST IMPORTANT METHOD
    //
    // This method tells the auto layout
    // You cannot calculate the collectionView content size in any other place,
    // because you run into race condition issues.
    // NOTE: Works for iOS 8 or later

    override func systemLayoutSizeFittingSize(targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
        // With autolayout enabled on collection view's cells we need to force a collection view relayout with the shown size (width)


        //self.collectionView.frame = CGRectMake(0, 0, targetSize.width, targetSize.height)
        self.collectionView.layoutIfNeeded()
        // If the cell's size has to be exactly the content
        // Size of the collection View, just return the
        // collectionViewLayout's collectionViewContentSize.
        return self.collectionView.collectionViewLayout.collectionViewContentSize()
    }

,这在我的CollectionViewCell中:

    class CollectionViewCell: UICollectionViewCell {

    @IBOutlet var imgView: UIImageView!


    override func awakeFromNib() {

        self.contentView.setNeedsLayout()
    }


}

在我的TableViewController中也是这样:

    // Enable automatic row auto layout calculations
self.tableView.rowHeight = UITableViewAutomaticDimension;
// Set the estimatedRowHeight to a non-0 value to enable auto layout.
self.tableView.estimatedRowHeight = 100

我的项目文件

解决方案

您的问题停留在这行

return self.collectionView.collectionViewLayout.collectionViewContentSize()

我已替换为

return CGSizeMake(self.collectionView.collectionViewLayout.collectionViewContentSize().width, 300);

不要崩溃,但是我敢肯定这不是您想要的,因此也许您可以通过此提示更好地了解问题

i was having this issue (UICollectionView inside of UITableViewCell - AutoLayout)

and after trying several solution which are available in the internet i came with this @Pablo Romeu's answer i tried his solution , now i got a TableView with collectionView cells in it (which can dynamically change its size according to content in it) but when am scrolling through my tableview its getting crashed with this error :

    2016-06-13 22:05:51.546 WishMeluck[3507:425240] the behavior of the UICollectionViewFlowLayout is not defined because:
2016-06-13 22:05:51.546 WishMeluck[3507:425240] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
2016-06-13 22:05:51.546 WishMeluck[3507:425240] Please check the values return by the delegate.
2016-06-13 22:05:51.547 WishMeluck[3507:425240] The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fb83c05de90>, and it is attached to <UICollectionView: 0x7fb83a0c7c00; frame = (0 82.5; 375 179); clipsToBounds = YES; autoresize = RM+BM; tag = 4; gestureRecognizers = <NSArray: 0x7fb83c05cf00>; layer = <CALayer: 0x7fb83c055fb0>; contentOffset: {0, 0}; contentSize: {539, 179}> collection view layout: <UICollectionViewFlowLayout: 0x7fb83c05de90>.
2016-06-13 22:05:51.547 WishMeluck[3507:425240] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
2016-06-13 22:05:51.548 WishMeluck[3507:425240] the behavior of the UICollectionViewFlowLayout is not defined because:
2016-06-13 22:05:51.548 WishMeluck[3507:425240] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
2016-06-13 22:05:51.548 WishMeluck[3507:425240] Please check the values return by the delegate.
2016-06-13 22:05:51.548 WishMeluck[3507:425240] The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fb83c05de90>, and it is attached to <UICollectionView: 0x7fb83a0c7c00; frame = (0 82.5; 375 179); clipsToBounds = YES; autoresize = RM+BM; tag = 4; gestureRecognizers = <NSArray: 0x7fb83c05cf00>; layer = <CALayer: 0x7fb83c055fb0>; contentOffset: {0, 0}; contentSize: {539, 179}> collection view layout: <UICollectionViewFlowLayout: 0x7fb83c05de90>.
2016-06-13 22:05:51.548 WishMeluck[3507:425240] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
(lldb) 

am not able to figure out the cause of this error if any body knows then please help me out

added this codes to my TableViewCell

        override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

//        collectionViewContainer.frame = self.bounds;
//        collectionViewContainer.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

        let flow: UICollectionViewFlowLayout = (self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout)
        // Configure the collectionView
        flow.minimumInteritemSpacing = 1
            // This enables the magic of auto layout.
            // Setting estimatedItemSize different to CGSizeZero
            // on flow Layout enables auto layout for collectionView cells.
            // https://developer.apple.com/videos/play/wwdc2014-226/
            flow.estimatedItemSize = CGSizeMake(1, 1)
        // Disable the scroll on your collection view
        // to avoid running into multiple scroll issues.
        self.collectionView.scrollEnabled = true
    }

    func bindWithModel(model: AnyObject) {
        // Do your stuff here to configure the tableViewCell
        // Tell the cell to redraw its contentView



        self.contentView.layoutIfNeeded()
    }

    // THIS IS THE MOST IMPORTANT METHOD
    //
    // This method tells the auto layout
    // You cannot calculate the collectionView content size in any other place,
    // because you run into race condition issues.
    // NOTE: Works for iOS 8 or later

    override func systemLayoutSizeFittingSize(targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
        // With autolayout enabled on collection view's cells we need to force a collection view relayout with the shown size (width)


        //self.collectionView.frame = CGRectMake(0, 0, targetSize.width, targetSize.height)
        self.collectionView.layoutIfNeeded()
        // If the cell's size has to be exactly the content
        // Size of the collection View, just return the
        // collectionViewLayout's collectionViewContentSize.
        return self.collectionView.collectionViewLayout.collectionViewContentSize()
    }

and this in my CollectionViewCell:

    class CollectionViewCell: UICollectionViewCell {

    @IBOutlet var imgView: UIImageView!


    override func awakeFromNib() {

        self.contentView.setNeedsLayout()
    }


}

also this in my TableViewController:

    // Enable automatic row auto layout calculations
self.tableView.rowHeight = UITableViewAutomaticDimension;
// Set the estimatedRowHeight to a non-0 value to enable auto layout.
self.tableView.estimatedRowHeight = 100

my project file

解决方案

your problem stay at this line

return self.collectionView.collectionViewLayout.collectionViewContentSize()

I had replaced with

return CGSizeMake(self.collectionView.collectionViewLayout.collectionViewContentSize().width, 300);

and don't crash but I am sure that this is not that you want so maybe you can see the problem better with this hint

这篇关于滚动其中具有Collectionview的TableView时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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