简体   繁体   English

使用ViewCellRenderer在iOS上的Xamarin表单中ViewCell的自动高度

[英]Automatic height of ViewCell in Xamarin Forms on iOS with ViewCellRenderer

For performance reasons I decided to write a custom ViewCell for my ListView in Xamarin Forms. 出于性能原因,我决定为Xamarin Forms中的ListView编写一个自定义ViewCell。 For this ViewCell I've extended the ViewCellRenderer for iOS. 对于此ViewCell,我扩展了适用于iOS的ViewCellRenderer。 In the GetCell-Method I create a new instance of an extended UITableViewCell which creates some labels and images and arranges them by some constraints on the anchors. 在GetCell-Method中,我创建了扩展的UITableViewCell的新实例,该实例创建了一些标签和图像,并通过锚点上的一些约束来排列它们。

The ListView in Xamarin Forms has HasUnevenRows="True", but the rows are all cut-off at 44px. Xamarin表单中的ListView具有HasUnevenRows =“ True”,但所有行均以44px截断。 How can I get automatic height to the rows by the created content from the custom UITableViewCell? 如何从自定义UITableViewCell获取所创建内容的行的自动高度?

You should override LayoutSubviews . 您应该重写LayoutSubviews You should set renderer.NativeView.Frame to proper values. 您应该将renderer.NativeView.Frame设置为适当的值。

Cause: 原因:

Actually, The auto height with HasUnevenRows=true works fine on iOS if not using a custom renderer. 实际上,如果不使用自定义渲染器,则具有HasUnevenRows=true的自动高度在iOS上可以正常工作。

If using a custom renderer, then it is up to the renderer to set the height of the cell , but in reality you have to do it in the GetHeightForRow override of the UITableViewSource . 如果使用自定义渲染器,则取决于渲染器来设置单元格的高度 ,但是实际上您必须在UITableViewSourceGetHeightForRow覆盖中进行设置。

This means you have to calculate the row height in UITableViewSource that you make that sets the height for a row in the GetHeightForRows override if you want to use custom viewcell render in the listView with uneven rows. 这意味着,如果要在具有不均匀行的listView中使用自定义viewcell渲染,则必须在UITableViewSource中计算行高,该行高将设置GetHeightForRows override的行的高度。

Solution: 解:

Basically you have to make a custom ListView renderer . 基本上,您必须制作一个自定义ListView renderer Then you have to get the UITableView's Source property (Control.Source in the list view renderer) and pass that to a new instance of a subclass of UITableVIewSource that you create, and then override all of the UITableViewSource methods so you can call into the original source methods when you don't want to change anything, but for the GetRowForHeight method, you would return the height you want for that row. 然后,您必须获取UITableView's Source属性(列表视图渲染器中的Control.Source),并将其传递给您创建的UITableVIewSource子类的新实例,然后覆盖所有UITableViewSource方法,以便可以调用原始方法。源方法,当您不想更改任何内容时,但是对于GetRowForHeight方法,您将返回该行所需的高度。 EG: 例如:

[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;
        }                
    }

This is a example of increasing the row height by ten for every row. 这是将每行的行高增加十的示例。 You have to calculate your own row height by yourself. 您必须自己计算自己的行高。 I can share you the demo if you need. 如果需要,我可以与您分享演示。

Refer: viewcell 参考: viewcell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM