Xamarin 自定义表视图标题 [英] Xamarin Custom Table View Headers

查看:33
本文介绍了Xamarin 自定义表视图标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在表格视图部分标题的标题中添加一个按钮,即加号按钮,经过研究发现要做到这一点,您必须创建一个自定义标题.我不知道如何做到这一点.

I was wanting to add a button to the header of a table view section header, i.e. a plus button, and after research found that to do that you must create a custom header. I am unable to find out how to do that.

如何在 xamarin 中为表格视图部分创建自定义标题?

How can you create a custom header to a table view section in xamarin?

我也在使用 Xaml 和 C#

I am using Xaml and C# as well

推荐答案

查看这些博客文章:https://alexdunn.org/2017/03/21/xamarin-tips-xamarin-forms-ios-custom-tableview-section-titles/https://alexdunn.org/2017/03/21/xamarin-tips-xamarin-forms-android-custom-tableview-section-titles/

他们描述了使用 TableView 的自定义渲染器来自定义部分标题.iOS 版本:

They describe using a custom renderer for the TableView to customize the section header. iOS Version:

在共享代码中:

public partial class ColoredTableView : TableView
{
   public static BindableProperty GroupHeaderColorProperty = BindableProperty.Create("GroupHeaderColor", typeof(Color), typeof(ColoredTableView), Color.White);
   public Color GroupHeaderColor
   {
       get
       {
           return (Color)GetValue(GroupHeaderColorProperty);
       }
       set
       {
           SetValue(GroupHeaderColorProperty, value);
       }
   }

   public ColoredTableView()
   {
       InitializeComponent();
   }
}

在 iOS 项目中:

[assembly: ExportRenderer(typeof(ColoredTableView),typeof(ColoredTableViewRenderer))]
namespace YOUR_IOS_NAMESPACE
{
    public class ColoredTableViewRenderer : TableViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
        {
            base.OnElementChanged(e);
            if (Control == null)
                return;

            var tableView = Control as UITableView;
            var coloredTableView = Element as ColoredTableView;
            tableView.WeakDelegate = new CustomHeaderTableModelRenderer(coloredTableView);
        }

        private class CustomHeaderTableModelRenderer : UnEvenTableViewModelRenderer
        {
            private readonly ColoredTableView _coloredTableView;
            public CustomHeaderTableModelRenderer(TableView model) : base(model)
            {
                _coloredTableView = model as ColoredTableView;
            }
            public override UIView GetViewForHeader(UITableView tableView, nint section)
            {
                return new UILabel()
                {
                    Text = TitleForHeader(tableView, section),
                    TextColor = _coloredTableView.GroupHeaderColor.ToUIColor(),
                    TextAlignment = UITextAlignment.Center
                };
            }
        }
    }
} 

但是我认为使用 ListView 或制作自己的部分(即一个部分没有为 TableView 设置标题,然后使用单元格可能是最简单的方法.要做到这一点,只需定义一个没有标题的部分并使用 ViewCells对于部分:

However I think using a ListView or making your own sections (i.e. one section with no tittle set for the TableView, and then use cells may be the easiest way. To do that, just define one section with no title and use ViewCells for the sections:

<TableView Intent="Data" 
           HasUnevenRows="true" >
    <TableRoot>
        <TableSection>
            <ViewCell >
                <StackLayout Orientation="Horizontal">
                    <Label x:Name="label1" Text="Section one" />
                    <Button Text="Button1" Clicked="Handle_Clicked" />
                </StackLayout>
            </ViewCell>
            <TextCell Text="TextCell" 
                      Detail="TextCell Detail" />
            <EntryCell Label="Entry Label" 
                       Text="EntryCell Text" />
            <SwitchCell Text="SwitchCell Text" />
            <ViewCell >
                <StackLayout Orientation="Horizontal">
                    <Label x:Name="label2" Text="Section Two" />
                    <Button Text="Button2" Clicked="Handle_Clicked" />
                </StackLayout>
            </ViewCell>
            <TextCell Text="TextCell" 
                      Detail="TextCell Detail" />
            <EntryCell Label="Entry Label" 
                       Text="EntryCell Text" />
            <SwitchCell Text="SwitchCell Text" />
        </TableSection>
    </TableRoot>
</TableView>

这篇关于Xamarin 自定义表视图标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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