Xamarin Forms - 如何创建自定义渲染来为 TableSection 提供默认的 iOS 页脚? [英] Xamarin Forms - How to create custom render to give TableSection the default iOS Footer?

查看:12
本文介绍了Xamarin Forms - 如何创建自定义渲染来为 TableSection 提供默认的 iOS 页脚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want to add the default iOS footer to the tablesection in the tableview in Xamarin Forms. How can I go about doing this? I assume a customer renderer but I'm only reading that this can be done for Tableview?

To show an example, the following image is from Xcode storyboards when I enter the footer text on a static cell.

解决方案

A Custom renderer is right, but you will have to use a custom renderer for the entire TableView. From there, you will need to override the TableViewModelRenderer and then the GetViewForFooter method in that ModelRenderer. Here's something that might help get you started:

public class FooterTableViewRenderer : TableViewRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
    {
        base.OnElementChanged(e);
        if (Control == null)
            return;

        var tableView = Control as UITableView;
        var formsTableView = Element as TableView;
        tableView.WeakDelegate = new CustomFooterTableViewModelRenderer (formsTableView);
    }


    private class CustomFooterTableViewModelRenderer : TableViewModelRenderer
    {
        public CustomFooterTableViewModelRenderer(TableView model) : base(model)
        {
        }

        public override UIView GetViewForFooter(UITableView tableView, nint section)
        {
            return new UILabel()
            {
                Text = TitleForFooter(tableView, section), // or use some other text here
                TextAlignment = UITextAlignment.Center
            };
        }

    }
}

As mentioned in my comments below, you can alternatively override other methods in your TableViewModelRenderer:

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

        public override string TitleForFooter(UITableView tableView, nint section)
        {
            return "This is the title for this given section";
        }

这篇关于Xamarin Forms - 如何创建自定义渲染来为 TableSection 提供默认的 iOS 页脚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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