当我上下滚动UICollectionView时,视图高度约束未正确设置 [英] View height constraints are not set correctly as I scroll UICollectionView up and down

查看:357
本文介绍了当我上下滚动UICollectionView时,视图高度约束未正确设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图基于模型属性确定视图高度,但是随着UICollectionView上下滚动,不正确的高度被分配给可见单元格.似乎在GetCell(即cellForItemAtIndexPath)中设置HeightAnchor无效.我该如何进行这项工作?

I'm trying to decide view heights based on a model property, but as UICollectionView is scrolled up and down, incorrect heights are assigned to visible cells. It seems that setting a HeightAnchor in GetCell (i.e. cellForItemAtIndexPath) does not work. How can I make this work?

using CoreGraphics;
using Foundation;
using System;
using System.Collections.Generic;
using UIKit;

namespace App2
{
    public partial class ViewController : UIViewController
    {
        private UICollectionView _collectionView;

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

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

            InitializeCollectionView();
        }

        private void InitializeCollectionView()
        {
            _collectionView = new UICollectionView(View.Frame, new UICollectionViewCompositionalLayout(GetSection()))
            {
                DataSource = new CustomUICollectionViewDataSource(),
                TranslatesAutoresizingMaskIntoConstraints = false
            };

            _collectionView.RegisterClassForCell(typeof(CustomUICollectionViewCell), "CustomUICollectionViewCell");

            View.AddSubview(_collectionView);

            NSLayoutConstraint.ActivateConstraints(new[]
            {
                _collectionView.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor),
                _collectionView.BottomAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.BottomAnchor),
                _collectionView.LeftAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.LeftAnchor),
                _collectionView.RightAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.RightAnchor)
            });
        }

        private static NSCollectionLayoutSection GetSection()
        {
            var size = NSCollectionLayoutSize.Create(NSCollectionLayoutDimension.CreateFractionalWidth(1), NSCollectionLayoutDimension.CreateEstimated(50));
            var item = NSCollectionLayoutItem.Create(size);
            var group = NSCollectionLayoutGroup.CreateHorizontal(layoutSize: size, subitem: item, count: 1);
            var section = NSCollectionLayoutSection.Create(group);

            section.InterGroupSpacing = 5;

            return section;
        }
    }

    public class CustomUICollectionViewDataSource : UICollectionViewDataSource
    {
        private readonly List<Model> _models = new List<Model>
        {
            new Model {Height = 250},
            new Model {Height = 100},
            new Model {Height = 300},
            new Model {Height = 400},
            new Model {Height = 500},
            new Model {Height = 50},
            new Model {Height = 230},
            new Model {Height = 100},
            new Model {Height = 600},
            new Model {Height = 310},
            new Model {Height = 150},
            new Model {Height = 220}
        };

        public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
        {
            var model = _models[(int)indexPath.Item];

            var cell = collectionView.DequeueReusableCell("CustomUICollectionViewCell", indexPath) as CustomUICollectionViewCell;

            cell.UpdateHeight(model.Height);

            return cell;
        }

        public override nint GetItemsCount(UICollectionView collectionView, nint section)
        {
            return _models.Count;
        }
    }

    public sealed class CustomUICollectionViewCell : UICollectionViewCell
    {
        private readonly UIView _uiView;

        [Export("initWithFrame:")]
        public CustomUICollectionViewCell(CGRect frame) : base(frame)
        {
            _uiView = new UIView
            {
                BackgroundColor = UIColor.Brown,
                TranslatesAutoresizingMaskIntoConstraints = false
            };

            ContentView.AddSubview(_uiView);

            NSLayoutConstraint.ActivateConstraints(new[]
            {
                _uiView.TopAnchor.ConstraintEqualTo(ContentView.SafeAreaLayoutGuide.TopAnchor),
                _uiView.BottomAnchor.ConstraintEqualTo(ContentView.SafeAreaLayoutGuide.BottomAnchor),
                _uiView.LeftAnchor.ConstraintEqualTo(ContentView.SafeAreaLayoutGuide.LeftAnchor),
                _uiView.RightAnchor.ConstraintEqualTo(ContentView.SafeAreaLayoutGuide.RightAnchor)
            });
        }

        public void UpdateHeight(int height)
        {
            _uiView.HeightAnchor.ConstraintEqualTo(height).Active = true;
        }
    }

    public class Model
    {
        public int Height { get; set; }
    }
}

推荐答案

这是 Xamarin支持:

NSLayoutConstraint heightConstraint;
public void UpdateHeight(int height)
{
    if (heightConstraint == null)
    {
        heightConstraint = _uiView.HeightAnchor.ConstraintEqualTo(height);
        heightConstraint.Active = true;
    }
    else
    {
        heightConstraint.Constant = height;
    }
}

这篇关于当我上下滚动UICollectionView时,视图高度约束未正确设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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