diff options
Diffstat (limited to 'Xamarin.Forms.Platform.iOS/Cells/CellRenderer.cs')
-rw-r--r-- | Xamarin.Forms.Platform.iOS/Cells/CellRenderer.cs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.iOS/Cells/CellRenderer.cs b/Xamarin.Forms.Platform.iOS/Cells/CellRenderer.cs new file mode 100644 index 00000000..c01a7774 --- /dev/null +++ b/Xamarin.Forms.Platform.iOS/Cells/CellRenderer.cs @@ -0,0 +1,77 @@ +using System; +#if __UNIFIED__ +using UIKit; +using Foundation; + +#else +using MonoTouch.UIKit; +using MonoTouch.Foundation; +#endif + +namespace Xamarin.Forms.Platform.iOS +{ + public class CellRenderer : IRegisterable + { + static readonly BindableProperty RealCellProperty = BindableProperty.CreateAttached("RealCell", typeof(UITableViewCell), typeof(Cell), null); + + EventHandler _onForceUpdateSizeRequested; + + public virtual UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv) + { + var tvc = reusableCell as CellTableViewCell ?? new CellTableViewCell(UITableViewCellStyle.Default, item.GetType().FullName); + + tvc.Cell = item; + + WireUpForceUpdateSizeRequested(item, tvc, tv); + + tvc.TextLabel.Text = item.ToString(); + + UpdateBackground(tvc, item); + return tvc; + } + + protected void UpdateBackground(UITableViewCell tableViewCell, Cell cell) + { + if (TemplatedItemsList<ItemsView<Cell>, Cell>.GetIsGroupHeader(cell)) + { + if (UIDevice.CurrentDevice.CheckSystemVersion(7, 0)) + tableViewCell.BackgroundColor = new UIColor(247f / 255f, 247f / 255f, 247f / 255f, 1); + } + else + { + // Must be set to a solid color or blending issues will occur + var bgColor = UIColor.White; + + var element = cell.RealParent as VisualElement; + if (element != null) + bgColor = element.BackgroundColor == Color.Default ? bgColor : element.BackgroundColor.ToUIColor(); + + tableViewCell.BackgroundColor = bgColor; + } + } + + protected void WireUpForceUpdateSizeRequested(Cell cell, UITableViewCell nativeCell, UITableView tableView) + { + cell.ForceUpdateSizeRequested -= _onForceUpdateSizeRequested; + + _onForceUpdateSizeRequested = delegate + { + var index = tableView.IndexPathForCell(nativeCell); + if (index != null) + tableView.ReloadRows(new[] { index }, UITableViewRowAnimation.None); + }; + + cell.ForceUpdateSizeRequested += _onForceUpdateSizeRequested; + } + + internal static UITableViewCell GetRealCell(BindableObject cell) + { + return (UITableViewCell)cell.GetValue(RealCellProperty); + } + + internal static void SetRealCell(BindableObject cell, UITableViewCell renderer) + { + cell.SetValue(RealCellProperty, renderer); + } + } +}
\ No newline at end of file |