Ios 在Xamarin本机中存在分区布局问题的UICollectionView

Ios 在Xamarin本机中存在分区布局问题的UICollectionView,ios,xamarin,xamarin.ios,Ios,Xamarin,Xamarin.ios,我有8个部分的UIcollectionView。每个部分都有一个专用阵列作为数据源。我想在按下按钮的每个部分添加一个项目。UICollectionView滚动方向设置为水平,因为我希望截面为垂直方向。 在Xcode projectXib和Objective-C中一切正常,因为这个屏幕截图中红色的项目是在按钮按下时添加的项目 当我在Xamarin ios中执行相同的实现时,按下下面的按钮后,布局变得一团糟 我的代码如下 public partial class GridViewControlle

我有8个部分的UIcollectionView。每个部分都有一个专用阵列作为数据源。我想在按下按钮的每个部分添加一个项目。UICollectionView滚动方向设置为水平,因为我希望截面为垂直方向。 在Xcode projectXib和Objective-C中一切正常,因为这个屏幕截图中红色的项目是在按钮按下时添加的项目

当我在Xamarin ios中执行相同的实现时,按下下面的按钮后,布局变得一团糟

我的代码如下

 public partial class GridViewController : UIViewController
{
    CVSource source;

    int MAX_NUMBER_OF_ROWS = 4;

    public GridViewController(IntPtr handle) : base(handle)
    {
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        // Perform any additional setup after loading the view, typically from a nib.
        //this.collectionView.SetNeedsDisplay();
        this.collectionView.RegisterClassForCell(typeof(ItemCell), ItemCell.CellId);

        source = new CVSource();
        this.collectionView.DataSource = source;


    }

    partial void AddButtonPressed(UIButton sender)
    {
        Console.WriteLine("Add button pressed");

        MAX_NUMBER_OF_ROWS = MAX_NUMBER_OF_ROWS + 1;
        source.AddItem();

        collectionView.Layer.RemoveAllAnimations();
        collectionView.ReloadData();

        ViewDidLayoutSubviews();

    }

    public override void ViewDidLayoutSubviews()
    {
        base.ViewDidLayoutSubviews();

        collectionView.Frame = new CoreGraphics.CGRect(collectionView.Frame.Location.X, collectionView.Frame.Location.Y, collectionView.Frame.Size.Width, MAX_NUMBER_OF_ROWS * 40);
        btnAdd.Frame = new CoreGraphics.CGRect(btnAdd.Frame.Location.X, collectionView.Frame.Location.Y + collectionView.Frame.Size.Height + 10, btnAdd.Frame.Size.Width, btnAdd.Frame.Size.Height);

    }

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

class CVSource : UICollectionViewSource
{

    List<string> arr1 = new List<string> { "40", "30"};
    List<string> arr2 = new List<string> { "25", "22" };
    List<string> arr3 = new List<string> { "35", "67" };
    List<string> arr4 = new List<string> { "26", "12" };
    List<string> arr5 = new List<string> { "27", "21", };
    List<string> arr6 = new List<string> { "12", "45" };
    List<string> arr7 = new List<string> { "34", "67" };
    List<string> arr8 = new List<string> { "21", "44", };


    public override nint NumberOfSections(UICollectionView collectionView)
    {
        return 8;
    }

    public override nint GetItemsCount(UICollectionView collectionView, nint section)
    {
        if ((int)section == 0)
            return arr1.Count;
        else if ((int)section == 1)
            return arr2.Count;
        else if ((int)section == 2)
            return arr3.Count;
        else if ((int)section == 3)
            return arr4.Count;
        else if ((int)section == 4)
            return arr5.Count;
        else if ((int)section == 5)
            return arr6.Count;
        else if ((int)section == 6)
            return arr7.Count;
        else if ((int)section == 7)
        return arr8.Count;

        return 0;
    }



    public override UICollectionViewCell GetCell(UICollectionView collectionView, Foundation.NSIndexPath indexPath)
    {
        var itemCell = (ItemCell)collectionView.DequeueReusableCell(ItemCell.CellId, indexPath);

        if (indexPath.Section == 0)
        {
            itemCell.SetText(arr1[indexPath.Row]);

        }
        else if (indexPath.Section == 1)
        {
            itemCell.SetText(arr2[indexPath.Row]);

        }
        else if (indexPath.Section == 2)
        {
            itemCell.SetText(arr3[indexPath.Row]);

        }
        else if (indexPath.Section == 3)
        {
            itemCell.SetText(arr4[indexPath.Row]);

        }
        else if (indexPath.Section == 4)
        {
            itemCell.SetText(arr5[indexPath.Row]);

        }
        else if (indexPath.Section == 5)
        {
            itemCell.SetText(arr6[indexPath.Row]);

        }
        else if (indexPath.Section == 6)
        {
            itemCell.SetText(arr7[indexPath.Row]);

        }
        else if (indexPath.Section == 7)
            itemCell.SetText(arr8[(indexPath.Row)]);


        return itemCell;
    }

    public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
    {
        Console.WriteLine("Row selected "+ indexPath.Row);
    }


    public void AddItem()
    {
        arr1.Add("0");
        arr2.Add("0");
        arr3.Add("0");
        arr4.Add("0");
        arr5.Add("0");
        arr6.Add("0");
        arr7.Add("0");
        arr8.Add("0");

    }
}
我不是Xamarin的开发人员,非常感谢Xamarin专家的任何意见/建议

如果您错过了为collectionView设置的UICollectionViewLayout,您可以为collectionView设置GridLayout进行尝试

this.collectionView.SetCollectionViewLayout(new GridLayout(), true); //add CollectionViewLayout
this.collectionView.RegisterClassForCell(typeof(ItemCell), ItemCell.CellId);
GridLayout继承自Xamrarin iOS中的UICollectionViewFlowLayout:

public class GridLayout : UICollectionViewFlowLayout
{
    public GridLayout ()
    {
    }

    public override bool ShouldInvalidateLayoutForBoundsChange (CGRect newBounds)
    {
        return true;
    }

    public override UICollectionViewLayoutAttributes LayoutAttributesForItem (NSIndexPath path)
    {
        return base.LayoutAttributesForItem (path);
    }

    public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect (CGRect rect)
    {
        return base.LayoutAttributesForElementsInRect (rect);
    }
}
其效果是:

Affer尝试了共享代码,效果如下:

仅在此处与您的代码不同:

 public ViewController (IntPtr handle) : base (handle)
{
    collectionView = new UICollectionView(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Size.Width, 300), new GridLayout());
    collectionView.RegisterClassForCell(typeof(ItemCell), ItemCell.CellId);

    //collectionView.SetCollectionViewLayout(new GridLayout(), true);
    collectionView.BackgroundColor = UIColor.Blue;
    source = new CVSource();
    collectionView.DataSource = source;
    View.AddSubview(collectionView);


    button = new UIButton(new CGRect(150, 400, 100, 50));
    button.SetTitle("Hello", UIControlState.Normal);
    button.BackgroundColor = UIColor.Red;
    View.AddSubview(button);

    button.TouchUpInside += Button_TouchUpInside;

}

注意:我使用代码创建接口,并为CollectionView设置GridLayout。这是。

Hi,我将检查哪个版本的visual studio。版本8.0.2build 23感谢您的建议,但GridLayout不适用于这种情况,因为这是一个包含8列的表。当用户按下Add按钮时,它应该向每列添加一个新行/项目。这就是我选择collectionview和sections的原因。@smartsanja好的,最新版本是8.4,您可以更新来试一试。我会在本地网站上查看你的共享代码。谢谢,我会在最新的VS上试用also@smartsanja我已经运行了你的代码,将更新gif作为回答。我不确定你想要什么效果,你可以通过视频分享本地效果,这会很有帮助。你的效果看起来不错。我不知道为什么在我身边不能正常工作。也许我的手机是罪魁祸首。我在原始问题中添加了ItemCell代码。请帮助指出其中是否存在任何问题
 public ViewController (IntPtr handle) : base (handle)
{
    collectionView = new UICollectionView(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Size.Width, 300), new GridLayout());
    collectionView.RegisterClassForCell(typeof(ItemCell), ItemCell.CellId);

    //collectionView.SetCollectionViewLayout(new GridLayout(), true);
    collectionView.BackgroundColor = UIColor.Blue;
    source = new CVSource();
    collectionView.DataSource = source;
    View.AddSubview(collectionView);


    button = new UIButton(new CGRect(150, 400, 100, 50));
    button.SetTitle("Hello", UIControlState.Normal);
    button.BackgroundColor = UIColor.Red;
    View.AddSubview(button);

    button.TouchUpInside += Button_TouchUpInside;

}