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
c# xaml xamarin xamarin.forms
add a comment |
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
c# xaml xamarin xamarin.forms
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
add a comment |
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
c# xaml xamarin xamarin.forms
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
c# xaml xamarin xamarin.forms
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
add a comment |
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
add a comment |
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>
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
add a comment |
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>
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
add a comment |
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>
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
add a comment |
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>
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>
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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