首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UICollectionView中的UICollectionView不会滚动

UICollectionView中的UICollectionView不会滚动
EN

Stack Overflow用户
提问于 2016-08-25 06:57:45
回答 1查看 343关注 0票数 1

我在另一个UICollectionView的每个单元中都有一个UICollectionView。外部的UICollectionView可以很好地垂直滚动,但是垂直UICollectionView中每个单元格内的UICollectionView不会滚动,我不知道为什么。

这是我的ViewController:

代码语言:javascript
运行
复制
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的单元格。

代码语言:javascript
运行
复制
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数据源的子类。

代码语言:javascript
运行
复制
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就是不能滚动。

EN

回答 1

Stack Overflow用户

发布于 2016-08-25 07:12:12

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39134244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档