Xamarin Custom Table View Headers











up vote
0
down vote

favorite












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










share|improve this question
























  • 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 7 at 23:35















up vote
0
down vote

favorite












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










share|improve this question
























  • 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 7 at 23:35













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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










share|improve this question















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







c# xaml xamarin xamarin.forms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 23:54









jgoldberger

2,9581723




2,9581723










asked Nov 7 at 22:41









Alessandro Farace

10219




10219












  • 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 7 at 23:35


















  • 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 7 at 23:35
















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 7 at 23:35




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 7 at 23:35












1 Answer
1






active

oldest

votes

















up vote
1
down vote













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>





share|improve this answer





















  • Thanks for help. Im struggling a little because adding the custom view cell would make it a different colour? Is there a way to make it so that the view cell looks identical to the background?
    – Alessandro Farace
    Nov 8 at 19:48











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199011%2fxamarin-custom-table-view-headers%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













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>





share|improve this answer





















  • Thanks for help. Im struggling a little because adding the custom view cell would make it a different colour? Is there a way to make it so that the view cell looks identical to the background?
    – Alessandro Farace
    Nov 8 at 19:48















up vote
1
down vote













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>





share|improve this answer





















  • Thanks for help. Im struggling a little because adding the custom view cell would make it a different colour? Is there a way to make it so that the view cell looks identical to the background?
    – Alessandro Farace
    Nov 8 at 19:48













up vote
1
down vote










up vote
1
down vote









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>





share|improve this answer












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>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 0:30









jgoldberger

2,9581723




2,9581723












  • Thanks for help. Im struggling a little because adding the custom view cell would make it a different colour? Is there a way to make it so that the view cell looks identical to the background?
    – Alessandro Farace
    Nov 8 at 19:48


















  • Thanks for help. Im struggling a little because adding the custom view cell would make it a different colour? Is there a way to make it so that the view cell looks identical to the background?
    – Alessandro Farace
    Nov 8 at 19:48
















Thanks for help. Im struggling a little because adding the custom view cell would make it a different colour? Is there a way to make it so that the view cell looks identical to the background?
– Alessandro Farace
Nov 8 at 19:48




Thanks for help. Im struggling a little because adding the custom view cell would make it a different colour? Is there a way to make it so that the view cell looks identical to the background?
– Alessandro Farace
Nov 8 at 19:48


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199011%2fxamarin-custom-table-view-headers%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini