UICollectionView 内的 UICollectionView 不会滚动 [英] UICollectionView inside UICollectionView will not scroll

查看:225
本文介绍了UICollectionView 内的 UICollectionView 不会滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一个 UICollectionView 的每个单元格中都有一个 UICollectionView.外部 UICollectionView 垂直滚动良好,但垂直 UICollectionView 的每个单元格内的 UICollectionView 不会滚动,我不知道为什么.

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.

这是我的视图控制器:

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 的单元格.

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);

    }




}
}

这是我的第二个 UICollectionView 数据源的子类.

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;
    }

}
}

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

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

推荐答案

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

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

这篇关于UICollectionView 内的 UICollectionView 不会滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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