1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
using System;
using System.ComponentModel;
using UIKit;
using RectangleF = CoreGraphics.CGRect;
using SizeF = CoreGraphics.CGSize;
namespace Xamarin.Forms.Platform.iOS
{
public class ViewCellRenderer : CellRenderer
{
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var viewCell = (ViewCell)item;
var cell = reusableCell as ViewTableCell;
if (cell == null)
cell = new ViewTableCell(item.GetType().FullName);
else
cell.ViewCell.PropertyChanged -= ViewCellPropertyChanged;
viewCell.PropertyChanged += ViewCellPropertyChanged;
cell.ViewCell = viewCell;
SetRealCell(item, cell);
WireUpForceUpdateSizeRequested(item, cell, tv);
UpdateBackground(cell, item);
UpdateIsEnabled(cell, viewCell);
return cell;
}
static void UpdateIsEnabled(ViewTableCell cell, ViewCell viewCell)
{
cell.UserInteractionEnabled = viewCell.IsEnabled;
cell.TextLabel.Enabled = viewCell.IsEnabled;
}
void ViewCellPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var viewCell = (ViewCell)sender;
var realCell = (ViewTableCell)GetRealCell(viewCell);
if (e.PropertyName == Cell.IsEnabledProperty.PropertyName)
UpdateIsEnabled(realCell, viewCell);
}
internal class ViewTableCell : UITableViewCell, INativeElementView
{
WeakReference<IVisualElementRenderer> _rendererRef;
ViewCell _viewCell;
Element INativeElementView.Element => ViewCell;
internal bool SupressSeparator { get; set; }
bool _disposed;
public ViewTableCell(string key) : base(UITableViewCellStyle.Default, key)
{
}
public ViewCell ViewCell
{
get { return _viewCell; }
set
{
if (_viewCell == value)
return;
UpdateCell(value);
}
}
public override void LayoutSubviews()
{
//This sets the content views frame.
base.LayoutSubviews();
//TODO: Determine how best to hide the separator line when there is an accessory on the cell
if (SupressSeparator && Accessory == UITableViewCellAccessory.None)
{
var oldFrame = Frame;
ContentView.Bounds = new RectangleF(oldFrame.Location, new SizeF(oldFrame.Width, oldFrame.Height + 0.5f));
}
var contentFrame = ContentView.Frame;
var view = ViewCell.View;
Layout.LayoutChildIntoBoundingRegion(view, contentFrame.ToRectangle());
if (_rendererRef == null)
return;
IVisualElementRenderer renderer;
if (_rendererRef.TryGetTarget(out renderer))
renderer.NativeView.Frame = view.Bounds.ToRectangleF();
}
public override SizeF SizeThatFits(SizeF size)
{
IVisualElementRenderer renderer;
if (!_rendererRef.TryGetTarget(out renderer))
return base.SizeThatFits(size);
if (renderer.Element == null)
return SizeF.Empty;
double width = size.Width;
var height = size.Height > 0 ? size.Height : double.PositiveInfinity;
var result = renderer.Element.Measure(width, height);
// make sure to add in the separator if needed
var finalheight = (float)result.Request.Height + (SupressSeparator ? 0f : 1f) / UIScreen.MainScreen.Scale;
return new SizeF(size.Width, finalheight);
}
protected override void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
IVisualElementRenderer renderer;
if (_rendererRef != null && _rendererRef.TryGetTarget(out renderer) && renderer.Element != null)
{
var platform = renderer.Element.Platform as Platform;
if (platform != null)
platform.DisposeModelAndChildrenRenderers(renderer.Element);
_rendererRef = null;
}
_viewCell = null;
}
_disposed = true;
base.Dispose(disposing);
}
IVisualElementRenderer GetNewRenderer()
{
var newRenderer = Platform.CreateRenderer(_viewCell.View);
_rendererRef = new WeakReference<IVisualElementRenderer>(newRenderer);
ContentView.AddSubview(newRenderer.NativeView);
return newRenderer;
}
void UpdateCell(ViewCell cell)
{
ICellController cellController = _viewCell;
if (cellController != null)
Device.BeginInvokeOnMainThread(cellController.SendDisappearing);
_viewCell = cell;
cellController = cell;
Device.BeginInvokeOnMainThread(cellController.SendAppearing);
IVisualElementRenderer renderer;
if (_rendererRef == null || !_rendererRef.TryGetTarget(out renderer))
renderer = GetNewRenderer();
else
{
if (renderer.Element != null && renderer == Platform.GetRenderer(renderer.Element))
renderer.Element.ClearValue(Platform.RendererProperty);
var type = Registrar.Registered.GetHandlerType(_viewCell.View.GetType());
if (renderer.GetType() == type || (renderer is Platform.DefaultRenderer && type == null))
renderer.SetElement(_viewCell.View);
else
{
//when cells are getting reused the element could be already set to another cell
//so we should dispose based on the renderer and not the renderer.Element
var platform = renderer.Element.Platform as Platform;
platform.DisposeRendererAndChildren(renderer);
renderer = GetNewRenderer();
}
}
Platform.SetRenderer(_viewCell.View, renderer);
}
}
}
}
|