Last active
January 15, 2018 10:45
-
-
Save veryhumble/4e6ed34a1a636accebec11e532b53da8 to your computer and use it in GitHub Desktop.
HeaderViewCell Renderer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Xamarin.Forms; | |
namespace A11YGuide.Controls | |
{ | |
public class HeaderViewCell : ViewCell | |
{ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using A11YGuide.Controls; | |
using A11YGuide.Controls.Lists; | |
using A11YGuide.iOS.Controls; | |
using A11YGuide.iOS.Controls.Lists; | |
using UIKit; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Platform.iOS; | |
[assembly: ExportRenderer(typeof(HeaderViewCell), typeof(HeaderViewCellRenderer))] | |
namespace A11YGuide.iOS.Controls | |
{ | |
public class HeaderViewCellRenderer : ViewCellRenderer | |
{ | |
static readonly BindableProperty RealCellProperty = BindableProperty.CreateAttached("RealCell", typeof(UITableViewCell), typeof(Cell), null); | |
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv) | |
{ | |
var viewCell = (ViewCell)item; | |
if (!(reusableCell is NativeHeaderViewCell cell)) | |
{ | |
cell = new NativeHeaderViewCell(item.GetType().FullName); | |
} | |
cell.ViewCell = viewCell; | |
SetRealCell(item, cell); | |
return cell; | |
} | |
private static void SetRealCell(BindableObject cell, UITableViewCell renderer) | |
{ | |
cell.SetValue(RealCellProperty, renderer); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using BigTed; | |
using UIKit; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Platform.iOS; | |
namespace A11YGuide.iOS.Controls.Lists | |
{ | |
public class NativeHeaderViewCell : UITableViewCell, INativeElementView | |
{ | |
WeakReference<IVisualElementRenderer> _rendererRef; | |
private ViewCell _viewCell; | |
public NativeHeaderViewCell(string key) : base(UITableViewCellStyle.Default, key) | |
{ | |
} | |
Element INativeElementView.Element => ViewCell; | |
public ViewCell ViewCell | |
{ | |
get => _viewCell; | |
set | |
{ | |
if (_viewCell == value) | |
return; | |
UpdateCell(value); | |
} | |
} | |
private void UpdateCell(ViewCell cell) | |
{ | |
ICellController cellController = ViewCell; | |
if (cellController != null) | |
Device.BeginInvokeOnMainThread(cellController.SendDisappearing); | |
_viewCell = cell; | |
cellController = cell; | |
Device.BeginInvokeOnMainThread(cellController.SendAppearing); | |
var renderer = GetNewRenderer(); | |
Platform.SetRenderer(ViewCell.View, renderer); | |
} | |
public override void LayoutSubviews() | |
{ | |
//This sets the content views frame. | |
base.LayoutSubviews(); | |
var contentFrame = ContentView.Frame; | |
var view = ViewCell.View; | |
Layout.LayoutChildIntoBoundingRegion(view, contentFrame.ToRectangle()); | |
if (_rendererRef == null) | |
return; | |
if (_rendererRef.TryGetTarget(out var renderer)) | |
{ | |
renderer.NativeView.Frame = view.Bounds.ToRectangleF(); | |
} | |
} | |
IVisualElementRenderer GetNewRenderer() | |
{ | |
var newRenderer = Platform.CreateRenderer(ViewCell.View); | |
_rendererRef = new WeakReference<IVisualElementRenderer>(newRenderer); | |
ContentView.AddSubview(newRenderer.NativeView); | |
return newRenderer; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ListView Grid.Row="0" | |
BackgroundColor="#162B2B" | |
SeparatorVisibility="None" | |
HasUnevenRows="True" | |
IsGroupingEnabled="True" | |
Header="{Binding}" | |
Footer="{Binding}" | |
VerticalOptions="StartAndExpand"> | |
<ListView.GroupHeaderTemplate> | |
<DataTemplate> | |
<controls1:HeaderViewCell Height="30"> | |
<Grid BackgroundColor="#162B2B" RowSpacing="0" /> | |
</controls1:HeaderViewCell> | |
</DataTemplate> | |
</ListView.GroupHeaderTemplate> | |
</ListView> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment