This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathTableViewRenderer.cs
170 lines (146 loc) · 4.39 KB
/
TableViewRenderer.cs
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
using System.Collections.Generic;
using System.ComponentModel;
using UIKit;
using RectangleF = CoreGraphics.CGRect;
namespace Xamarin.Forms.Platform.iOS
{
public class TableViewRenderer : ViewRenderer<TableView, UITableView>
{
const int DefaultRowHeight = 44;
KeyboardInsetTracker _insetTracker;
UIView _originalBackgroundView;
RectangleF _previousFrame;
[Internals.Preserve(Conditional = true)]
public TableViewRenderer()
{
}
public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
{
return Control.GetSizeRequest(widthConstraint, heightConstraint, DefaultRowHeight, DefaultRowHeight);
}
public override void LayoutSubviews()
{
_insetTracker?.OnLayoutSubviews();
base.LayoutSubviews();
if (_previousFrame != Frame)
{
_previousFrame = Frame;
_insetTracker?.UpdateInsets();
}
}
protected override void Dispose(bool disposing)
{
if (disposing && _insetTracker != null)
{
_insetTracker.Dispose();
_insetTracker = null;
var viewsToLookAt = new Stack<UIView>(Subviews);
while (viewsToLookAt.Count > 0)
{
var view = viewsToLookAt.Pop();
var viewCellRenderer = view as ViewCellRenderer.ViewTableCell;
if (viewCellRenderer != null)
viewCellRenderer.Dispose();
else
{
foreach (var child in view.Subviews)
viewsToLookAt.Push(child);
}
}
}
base.Dispose(disposing);
}
protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
{
if (e.NewElement != null)
{
var style = UITableViewStyle.Plain;
if (e.NewElement.Intent != TableIntent.Data)
style = UITableViewStyle.Grouped;
if (Control == null || Control.Style != style)
{
if (Control != null)
{
_insetTracker.Dispose();
Control.Dispose();
}
var tv = new UITableView(RectangleF.Empty, style);
_originalBackgroundView = tv.BackgroundView;
SetNativeControl(tv);
if (Forms.IsiOS9OrNewer)
tv.CellLayoutMarginsFollowReadableWidth = false;
_insetTracker = new KeyboardInsetTracker(tv, () => Control.Window, insets => Control.ContentInset = Control.ScrollIndicatorInsets = insets, point =>
{
var offset = Control.ContentOffset;
offset.Y += point.Y;
Control.SetContentOffset(offset, true);
}, this);
}
SetSource();
UpdateRowHeight();
UpdateEstimatedRowHeight();
UpdateBackgroundView();
}
base.OnElementChanged(e);
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == TableView.RowHeightProperty.PropertyName)
UpdateRowHeight();
else if (e.PropertyName == TableView.HasUnevenRowsProperty.PropertyName)
SetSource();
else if (e.PropertyName == TableView.BackgroundColorProperty.PropertyName)
UpdateBackgroundView();
}
protected override void UpdateNativeWidget()
{
if (Element.Opacity < 1)
{
if (!Control.Layer.ShouldRasterize)
{
Control.Layer.RasterizationScale = UIScreen.MainScreen.Scale;
Control.Layer.ShouldRasterize = true;
}
}
else
Control.Layer.ShouldRasterize = false;
base.UpdateNativeWidget();
}
public override void TraitCollectionDidChange(UITraitCollection previousTraitCollection)
{
base.TraitCollectionDidChange(previousTraitCollection);
#if __XCODE11__
// Make sure the cells adhere to changes UI theme
if (Forms.IsiOS13OrNewer && previousTraitCollection?.UserInterfaceStyle != TraitCollection.UserInterfaceStyle)
Control.ReloadData();
#endif
}
void SetSource()
{
var modeledView = Element;
Control.Source = modeledView.HasUnevenRows ? new UnEvenTableViewModelRenderer(modeledView) : new TableViewModelRenderer(modeledView);
}
void UpdateBackgroundView()
{
Control.BackgroundView = Element.BackgroundColor == Color.Default ? _originalBackgroundView : null;
}
void UpdateRowHeight()
{
var rowHeight = Element.RowHeight;
if (Element.HasUnevenRows && rowHeight == -1) {
Control.RowHeight = UITableView.AutomaticDimension;
} else
Control.RowHeight = rowHeight <= 0 ? DefaultRowHeight : rowHeight;
}
void UpdateEstimatedRowHeight()
{
var rowHeight = Element.RowHeight;
if (Element.HasUnevenRows && rowHeight == -1) {
Control.EstimatedRowHeight = DefaultRowHeight;
} else {
Control.EstimatedRowHeight = 0;
}
}
}
}