UICollectionView内的UICollectionView不会滚动

时间:2016-08-24 22:57:45

标签: c# scroll xamarin.ios uicollectionview

我在另一个UICollectionView的每个单元格中都有一个UICollectionView。外部UICollectionView滚动精细垂直,但是每个单元格内的UICollectionView从垂直UICollectionView都不会滚动,而且我也不知道为什么。

这是我的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;
    }
}

}

这是第一个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);

    }




}
}

这是我的第二个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;
    }

}
}

布局看起来就像我想要的那样,但第二个UICollectionView只是不会滚动。

enter image description here

1 个答案:

答案 0 :(得分:0)

ColTwo应该添加到ContentView而不是BackgroundView。像这样 - ContentView.AddSubview(ColTwo)