当我上下滚动 UICollectionView 时,视图高度约束设置不正确

编程入门 行业动态 更新时间:2024-10-15 20:22:52
本文介绍了当我上下滚动 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 时,视图高度约束设置不正确

本文发布于:2023-11-25 00:23:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627540.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:当我   视图   不正确   高度   UICollectionView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!