【问题标题】:Dynamic Cell Height in MvvmCrossMvvmCross 中的动态单元格高度
【发布时间】:2015-06-30 21:18:51
【问题描述】:

我想重写 GetHeightForRow 方法,因此当滚动到特定单元格(行)时,我的滚动效果很好。

我发现这个例子here 运行良好。因为这部分代码:

 public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
    {
        if (this.offscreenCell == null)
        {
            this.offscreenCell = new ItemCell();
        }

        var cell = this.offscreenCell;

        cell.UpdateFonts();
        var item = this.model.Items[indexPath.Row];
        cell.Title = item.Title;
        cell.Body = item.Body;

        cell.SetNeedsUpdateConstraints();
        cell.UpdateConstraintsIfNeeded();

        cell.Bounds = new RectangleF(0, 0, this.TableView.Bounds.Width, this.TableView.Bounds.Height);

        cell.SetNeedsLayout();
        cell.LayoutIfNeeded();

        var height = cell.ContentView.SystemLayoutSizeFittingSize(UIView.UILayoutFittingCompressedSize).Height;
        height += 1;

        return height;
    }

尝试将其翻译到我的 MvvmCross 项目中,例如:

public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
    {
        if (this.offscreenCell == null)
        {
            this.offscreenCell = new ItemCell();
        }

        var cell = this.offscreenCell;
        cell.DataContext = this.model.Items[indexPath.Row];

        cell.SetNeedsUpdateConstraints();
        cell.UpdateConstraintsIfNeeded();

        cell.Bounds = new RectangleF(0, 0, this.TableView.Bounds.Width, this.TableView.Bounds.Height);

        cell.SetNeedsLayout();
        cell.LayoutIfNeeded();

        var height = cell.ContentView.SystemLayoutSizeFittingSize(UIView.UILayoutFittingCompressedSize).Height;
        height += 1;

        return height;
    }

但无论如何它总是返回 36.5。有什么制作方法吗?

谢谢你,

【问题讨论】:

    标签: ios dynamic xamarin height mvvmcross


    【解决方案1】:

    使用 UICollectionView 而不是 UITableView 可能会更好。总的来说,它们比表格视图更灵活。

    如果你试一试,你会发现为单个项目自定义高度非常容易(集合视图有项目而不是单元格)。

    首先,您需要创建一个自定义 UICollectionViewDelegateFlowLayout:

    public class ThingDelegateFlowLayout : UICollectionViewDelegateFlowLayout
    {
        List<Thing> Things;
    
        public ThingDelegateFlowLayout(List<Thing> things)
        {
            Things = things;
        }
    
        // this is the override that allows you to dynamically size UICollectionView items
        public override SizeF GetSizeForItem (UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
        {
            return new SizeF(
                width: UIScreen.MainScreen.Bounds.Width, // Set the width to whatever you want. In this case, it's the screen width.
                height: Things[indexPath.Row].Height // Set the item height
            );
        }
    }
    

    然后在您的视图控制器中执行以下操作。注意重要的一行...

    thingCollectionView.Delegate = new ThingDelegateFlowLayout(Things);

    ...在底部附近,您将自定义 UICollectionViewDelegateFlowLayout 分配给 UICollectionView:

    public class ThingViewController : UIViewController
    {
        private List<Thing> Things;
    
        public ThingViewController()
        {
            Things = GetThings();
        }
    
        // gets a list of sweet hair dos
        private List<Thing> GetThings()
        {
            // do something here to get a sweet list of hair dos
        }
    
        public override void ViewDidLoad()
        {
            UICollectionViewFlowLayout layout = new UICollectionViewFlowLayout () 
            {
                HeaderReferenceSize = new SizeF(0, 0), // no header
                FooterReferenceSize = new SizeF(0, 0), // no footer
                SectionInset = new UIEdgeInsets(0, 0, 0, 0), // no section inset
                ItemSize = new SizeF(UIScreen.MainScreen.Bounds.Width, 100f), // set each item to the full width of the screen and 100 pixels tall
                ScrollDirection = UICollectionViewScrollDirection.Vertical, // set the collection to scroll vertically
                MinimumInteritemSpacing = 0, // no item spacing
                MinimumLineSpacing = 0, // no line spacing
            };
    
            var thingCollectionViewController = new UICollectionViewController(layout);
            var thingCollectionView = new UICollectionView(UIScreen.MainScreen.Bounds.Size);
    
            thingCollectionViewController.CollectionView.Frame = thingCollectionView.Frame;
            thingCollectionView = thingCollectionViewController.CollectionView;
            thingCollectionView.Delegate = new ThingDelegateFlowLayout(Things);
            thingCollectionView.BackgroundColor = UIColor.Clear; // or whatever
            thingCollectionView.BackgroundView = new UIView () 
            {
                BackgroundColor = UIColor.Clear // or whatever
            };
    
            View.AddSubview (thingCollectionView);
        }
    }
    

    我不知道如何使用 UITableView 做到这一点。我基本上已经停止使用表格视图了,因为我更喜欢 UICollectionViews。

    【讨论】:

    • 这会导致我面临同样的问题。如果我想要一个具有动态高度的视图控制器单元格,我将不得不在 thingdelegateflowlayout.getsizeforitem 中计算它......问题是如何用 mvxtableviewcell 计算它......
    猜你喜欢
    • 2014-06-23
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    相关资源
    最近更新 更多