summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.iOS/Renderers/TableViewRenderer.cs
blob: a77b32cc13e03752d44176033a378a7fef1dee71 (plain)
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
using System;
using System.ComponentModel;
using System.Drawing;
using System.Collections.Generic;
#if __UNIFIED__
using UIKit;
using RectangleF = CoreGraphics.CGRect;

#else
using MonoTouch.UIKit;
#endif

namespace Xamarin.Forms.Platform.iOS
{
	public class TableViewRenderer : ViewRenderer<TableView, UITableView>
	{
		KeyboardInsetTracker _insetTracker;
		UIView _originalBackgroundView;
		RectangleF _previousFrame;

		public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
		{
			return Control.GetSizeRequest(widthConstraint, heightConstraint, 44, 44);
		}

		public override void LayoutSubviews()
		{
			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);
					});
				}

				SetSource();
				UpdateRowHeight();

				UpdateBackgroundView();
			}

			base.OnElementChanged(e);
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged(sender, e);

			if (e.PropertyName == "RowHeight")
				UpdateRowHeight();
			else if (e.PropertyName == "HasUnevenRows")
				SetSource();
			else if (e.PropertyName == "BackgroundColor")
				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();
		}

		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 height = Element.RowHeight;

			if (height <= 0)
				height = 44;

			Control.RowHeight = height;
		}
	}
}