ios - UICollectionView 与 Xamarin Native 中的部分布局问题

标签 ios xamarin xamarin.ios

我有一个包含 8 个部分的 UIcollectionView。每个部分都有一个专用数组作为数据源。我想在按下按钮时向每个部分添加一个项目。 UICollectionView 滚动方向设置为水平,因为我希望这些部分是垂直方向。 Xcode 项目(Xib 和 Objective-C)中的一切工作正常,如此屏幕截图所示(红色项目是按下按钮时添加的项目)。

enter image description here

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

enter image description here

我的代码如下

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

    }
}

ItemCell 类

public partial class ItemCell : UICollectionViewCell
{
    public static readonly NSString CellId = new NSString("ItemCell");
    public static UITextField txtFld;

    [Export("initWithFrame:")]

    public ItemCell(CGRect frame) : base(frame)
    {
        BackgroundView = new UIView { BackgroundColor = UIColor.Orange };

        SelectedBackgroundView = new UIView { BackgroundColor = UIColor.Green };

        ContentView.Layer.BorderColor = UIColor.LightGray.CGColor;
        ContentView.Layer.BorderWidth = 2.0f;
        ContentView.BackgroundColor = UIColor.White;
        ContentView.Transform = CGAffineTransform.MakeScale(0.8f, 0.8f);

        txtFld = new UITextField() { Frame = new CGRect(5.0, 5.0, 60.0, 30.0), KeyboardType = UIKeyboardType.DecimalPad };

    }
}

我不是 Xamarin 开发人员,感谢 Xamarin 专家的任何评论/建议

最佳答案

也许你错过了 UICollectionViewLayoutcollectionView 设置,你可以为 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);
    }
}

效果:

enter image description here

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

enter image description here

与您的代码的不同之处仅在于:

 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;

}

注意:我使用代码创建 Interface ,并为 CollectionView 设置 GridLayout 。这里是 sample link .

关于ios - UICollectionView 与 Xamarin Native 中的部分布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59772684/

相关文章:

ios - 如何在 iOS 版 MonoTouch/Xamarin 中导航命名空间?

c# - 无法在 Xamarin 项目的 PCL 项目中使用 fileInfo 和 fileStream

visual-studio - Visual Studio 中的 Apple ID 双重身份验证

ios - 如何仅刷新/重绘 uicollectionview 中的补充 View ?

ios - 呈现 SKStoreProductViewController 重新激活非事件约束

iphone - CLLocationManager startUpdatingLocation 不工作

ios - 在 iOS 上使用 Crashlytics 是否需要 Firebase pod?

ios - 如何修改标签的字体大小

Java Opencv 未检测轮廓。 imgproc.Drawcontours()

c# - MonoTouch 对话框 StringElements 可变单元格高度