UICollectionView inside UICollectionView will not

2019-08-01 05:10发布

问题:

I have a UICollectionView in each cell of another UICollectionView. The outer UICollectionView scroll fine vertical, but the UICollectionView inside each cell from the vertical UICollectionView will not scroll, and I have no clue why.

Here is my ViewController:

using System;
using Foundation;
using UIKit;

namespace ColInColTest
{
    public partial class ViewController : UIViewController, IUICollectionViewSource, IUICollectionViewDelegate
    {
        UICollectionViewFlowLayout LayoutOne;
        UICollectionView ColOne;

        protected ViewController(IntPtr handle) : base(handle)
        {
        } 



    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        // Perform any additional setup after loading the view, typically from a nib.

        LayoutOne = new UICollectionViewFlowLayout();
        LayoutOne.ScrollDirection = UICollectionViewScrollDirection.Vertical;
        LayoutOne.SectionInset = new UIEdgeInsets(0, 0, 0, 0);
        LayoutOne.ItemSize = new CoreGraphics.CGSize(View.Bounds.Width, 100);

        ColOne = new UICollectionView(CoreGraphics.CGRect.Empty, LayoutOne);
        ColOne.Frame = new CoreGraphics.CGRect(0, 0, View.Bounds.Width, View.Bounds.Height);
        ColOne.BackgroundColor = UIColor.Red;
        ColOne.Delegate = this;
        ColOne.DataSource = this;
        ColOne.RegisterClassForCell(typeof(CellOne), "cell1");

        View.AddSubview(ColOne);


    }

    public override void DidReceiveMemoryWarning()
    {
        base.DidReceiveMemoryWarning();
        // Release any cached data, images, etc that aren't in use.
    }

    [Export("numberOfSectionsInCollectionView:")]
    public nint NumberOfSections(UICollectionView collectionView)
    {
        return 1;
    }

    public nint GetItemsCount(UICollectionView collectionView, nint section)
    {
        return 10;
    }

    public UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
    {
        var cell = (CellOne)collectionView.DequeueReusableCell("cell1", indexPath);
        return cell;
    }
}

}

Here is my cell for the first UICollectionView.

using System;
using CoreGraphics;
using Foundation;
using UIKit;

namespace ColInColTest
{
    public partial class CellOne : UICollectionViewCell
    {

    UICollectionViewFlowLayout LayoutTwo;
    UICollectionView ColTwo;

    [Export("initWithFrame:")]
    public CellOne(CGRect frame) : base(frame)
    {
        BackgroundView = new UIView { BackgroundColor = UIColor.Orange };

        LayoutTwo = new UICollectionViewFlowLayout();
        LayoutTwo.ScrollDirection = UICollectionViewScrollDirection.Horizontal;
        LayoutTwo.SectionInset = new UIEdgeInsets(0, 0, 0, 0);
        LayoutTwo.ItemSize = new CoreGraphics.CGSize(100, this.Bounds.Height);

        ColTwo = new UICollectionView(CoreGraphics.CGRect.Empty, LayoutTwo);
        ColTwo.Frame = new CoreGraphics.CGRect(0, 0, BackgroundView.Bounds.Width, BackgroundView.Bounds.Height);
        ColTwo.BackgroundColor = UIColor.White;
        //ColTwo.Delegate = this;
        ColTwo.DataSource = new CellTwoSource();
        ColTwo.ScrollEnabled = true;
        ColTwo.RegisterClassForCell(typeof(CellTwo), "cell2");


        BackgroundView.AddSubview(ColTwo);

    }




}
}

Here is my subclass for the datasource of the second UICollectionView.

using System;

using Foundation;
using UIKit;

namespace ColInColTest
{
public partial class CellTwoSource : UICollectionViewSource
{
    public override nint NumberOfSections(UICollectionView collectionView)
    {
        return 1;
    }

    public override nint GetItemsCount(UICollectionView collectionView, nint section)
    {
        return 20;
    }

    public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
    {
        var cell = (CellTwo)collectionView.DequeueReusableCell("cell2", indexPath);
        return cell;
    }

}
}

The layout look just as I want it to, but the second UICollectionView just won't scroll.

回答1:

The ColTwo should be added to the ContentView and not BackgroundView. Like this - ContentView.AddSubview(ColTwo)