diff options
Diffstat (limited to 'Xamarin.Forms.Platform.iOS/Cells/CellTableViewCell.cs')
-rw-r--r-- | Xamarin.Forms.Platform.iOS/Cells/CellTableViewCell.cs | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.iOS/Cells/CellTableViewCell.cs b/Xamarin.Forms.Platform.iOS/Cells/CellTableViewCell.cs new file mode 100644 index 00000000..a9265868 --- /dev/null +++ b/Xamarin.Forms.Platform.iOS/Cells/CellTableViewCell.cs @@ -0,0 +1,96 @@ +using System; +using System.ComponentModel; +#if __UNIFIED__ +using UIKit; + +#else +using MonoTouch.UIKit; +#endif + +namespace Xamarin.Forms.Platform.iOS +{ + public class CellTableViewCell : UITableViewCell, INativeElementView + { + Cell _cell; + + public Action<object, PropertyChangedEventArgs> PropertyChanged; + + public CellTableViewCell(UITableViewCellStyle style, string key) : base(style, key) + { + } + + public Cell Cell + { + get { return _cell; } + set + { + if (_cell == value) + return; + + if (_cell != null) + Device.BeginInvokeOnMainThread(_cell.SendDisappearing); + _cell = value; + if (_cell != null) + Device.BeginInvokeOnMainThread(_cell.SendAppearing); + } + } + + public Element Element => Cell; + + public void HandlePropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (PropertyChanged != null) + PropertyChanged(this, e); + } + + internal static UITableViewCell GetNativeCell(UITableView tableView, Cell cell, bool recycleCells = false, string templateId = "") + { + var id = cell.GetType().FullName; + + var renderer = (CellRenderer)Registrar.Registered.GetHandler(cell.GetType()); + + ContextActionsCell contextCell = null; + UITableViewCell reusableCell = null; + if (cell.HasContextActions || recycleCells) + { + contextCell = (ContextActionsCell)tableView.DequeueReusableCell(ContextActionsCell.Key + templateId); + if (contextCell == null) + { + contextCell = new ContextActionsCell(templateId); + reusableCell = tableView.DequeueReusableCell(id); + } + else + { + contextCell.Close(); + reusableCell = contextCell.ContentCell; + + if (reusableCell.ReuseIdentifier.ToString() != id) + reusableCell = null; + } + } + else + reusableCell = tableView.DequeueReusableCell(id); + + var nativeCell = renderer.GetCell(cell, reusableCell, tableView); + + var cellWithContent = nativeCell; + + // Sometimes iOS for returns a dequeued cell whose Layer is hidden. + // This prevents it from showing up, so lets turn it back on! + if (cellWithContent.Layer.Hidden) + cellWithContent.Layer.Hidden = false; + + if (contextCell != null) + { + contextCell.Update(tableView, cell, nativeCell); + nativeCell = contextCell; + } + + // Because the layer was hidden we need to layout the cell by hand + if (cellWithContent != null) + cellWithContent.LayoutSubviews(); + + return nativeCell; + } + } +}
\ No newline at end of file |