【问题标题】:Automatic height of ViewCell in Xamarin Forms on iOS with ViewCellRenderer使用 ViewCellRenderer 在 iOS 上的 Xamarin Forms 中自动调整 ViewCell 的高度
【发布时间】:2019-03-20 10:49:54
【问题描述】:

出于性能原因,我决定在 Xamarin Forms 中为我的 ListView 编写一个自定义 ViewCell。对于这个 ViewCell,我为 iOS 扩展了 ViewCellRenderer。在 GetCell 方法中,我创建了一个扩展 UITableViewCell 的新实例,它创建了一些标签和图像,并通过锚点上的一些约束来排列它们。

Xamarin Forms 中的 ListView 具有 HasUnevenRows="True",但行都在 44 像素处被截断。如何通过自定义 UITableViewCell 创建的内容获取行的自动高度?

【问题讨论】:

  • 您解决了您的问题还是尝试了我的解决方案?

标签: ios forms listview xamarin


【解决方案1】:

原因:

实际上,如果不使用自定义渲染器,HasUnevenRows=true 的自动高度在 iOS 上可以正常工作。

如果使用自定义渲染器,则由渲染器设置单元格的高度,但实际上您必须在 GetHeightForRow 覆盖中完成UITableViewSource 的。

这意味着您必须计算 UITableViewSource 中的行高,以设置 GetHeightForRows override 中行的高度,如果您想在行不均匀的 listView 中使用自定义视图单元渲染。

解决方案:

基本上您必须自定义ListView renderer。然后,您必须获取 UITableView's Source 属性(列表视图渲染器中的 Control.Source)并将其传递给您创建的 UITableVIewSource 子类的新实例,然后覆盖所有 UITableViewSource 方法,以便当您不想更改任何内容时,可以调用原始源方法,但对于 GetRowForHeight 方法,您将返回该行所需的高度。例如:

[assembly: ExportRenderer(typeof(ListView), typeof(MyLVRenderer))]
public class MyLVRenderer : ListViewRenderer
    {
        //UITableViewSource originalSource;

        protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
        {
            base.OnElementChanged(e);

            UITableViewSource originalSource = (UIKit.UITableViewSource)Control.Source;
            Control.Source = new MyLVSource(originalSource);

        }
    }

    public class MyLVSource : UITableViewSource
    {
        UITableViewSource originalSource;

        public MyLVSource(UITableViewSource origSource)
        {
            originalSource = origSource;
        }

        public override nint RowsInSection(UITableView tableview, nint section)
        {
            return originalSource.RowsInSection(tableview, section);
        }

        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            return originalSource.GetCell(tableView, indexPath);
        }

        public override nfloat GetHeightForFooter(UITableView tableView, nint section)
        {
            return originalSource.GetHeightForFooter(tableView, section);
        }

        public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
        {
            nfloat origHeight = originalSource.GetHeightForRow(tableView, indexPath);

            // calculate your own row height here
            return origHeight + (indexPath.Row +1) * 10;
        }                
    }

这是每行将行高增加 10 的示例。您必须自己计算自己的行高。如果你需要,我可以给你分享演示。

参考:viewcell

【讨论】:

  • 完美——就是这样!
【解决方案2】:

您应该覆盖LayoutSubviews。您应该将renderer.NativeView.Frame 设置为适当的值。

【讨论】:

  • 感谢您的回答! renderer 到底是什么?我在LayoutSubviews 中尝试了几种方法: 1. this.Frame = new CoreGraphics.CGRect(...) 2. this.ViewCell.Height = ... 3. this.ContentView.Frame = new CoreGraphics.CGRect(...) 所有有和没有this.ViewCell.ForceUpdateSize() - 没有机会手动设置高度。
  • 你需要看看 Xamarin 是怎么做的,关键就在那一行,github.com/xamarin/Xamarin.Forms/blob/master/…
  • 嗯,问题是在我的情况下ViewCell.Viewnull,因此无法创建渲染器。
  • 你有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-22
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多