2

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.

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

I am using Xaml and C# as well

jgoldberger - MSFT
  • 5,978
  • 2
  • 20
  • 44
  • 1
    I assume you mean a Xamarin Forms TableView. It doesn't really support headers, just sections, and I don't think they can be customized. You could create a custom ViewCell to act as a header, or write a Custom Renderer. – Jason Nov 07 '18 at 23:35

1 Answers1

3

See these blog posts: 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/

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

In Shared code:

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();
   }
}

IN iOS project:

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

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>
jgoldberger - MSFT
  • 5,978
  • 2
  • 20
  • 44