This repository was 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 pathSwitchCellRenderer.cs
101 lines (82 loc) · 2.77 KB
/
SwitchCellRenderer.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
using System;
using System.ComponentModel;
using System.Drawing;
using UIKit;
namespace Xamarin.Forms.Platform.iOS
{
public class SwitchCellRenderer : CellRenderer
{
const string CellName = "Xamarin.SwitchCell";
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var tvc = reusableCell as CellTableViewCell;
UISwitch uiSwitch = null;
if (tvc == null)
tvc = new CellTableViewCell(UITableViewCellStyle.Value1, CellName);
else
{
uiSwitch = tvc.AccessoryView as UISwitch;
tvc.Cell.PropertyChanged -= OnCellPropertyChanged;
}
SetRealCell(item, tvc);
if (uiSwitch == null)
{
uiSwitch = new UISwitch(new RectangleF());
uiSwitch.ValueChanged += OnSwitchValueChanged;
tvc.AccessoryView = uiSwitch;
}
var boolCell = (SwitchCell)item;
tvc.Cell = item;
tvc.Cell.PropertyChanged += OnCellPropertyChanged;
tvc.AccessoryView = uiSwitch;
tvc.TextLabel.Text = boolCell.Text;
uiSwitch.On = boolCell.On;
WireUpForceUpdateSizeRequested(item, tvc, tv);
UpdateBackground(tvc, item);
UpdateIsEnabled(tvc, boolCell);
UpdateFlowDirection(tvc, boolCell);
return tvc;
}
void OnCellPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var boolCell = (SwitchCell)sender;
var realCell = (CellTableViewCell)GetRealCell(boolCell);
if (e.PropertyName == SwitchCell.OnProperty.PropertyName)
((UISwitch)realCell.AccessoryView).SetState(boolCell.On, true);
else if (e.PropertyName == SwitchCell.TextProperty.PropertyName)
realCell.TextLabel.Text = boolCell.Text;
else if (e.PropertyName == Cell.IsEnabledProperty.PropertyName)
UpdateIsEnabled(realCell, boolCell);
else if (e.PropertyName == VisualElement.FlowDirectionProperty.PropertyName)
UpdateFlowDirection(realCell, boolCell);
}
void OnSwitchValueChanged(object sender, EventArgs eventArgs)
{
var view = (UIView)sender;
var sw = (UISwitch)view;
CellTableViewCell realCell = null;
while (view.Superview != null && realCell == null)
{
view = view.Superview;
realCell = view as CellTableViewCell;
}
if (realCell != null)
((SwitchCell)realCell.Cell).On = sw.On;
}
void UpdateFlowDirection(CellTableViewCell cell, SwitchCell switchCell)
{
IVisualElementController controller = switchCell.Parent as View;
var uiSwitch = cell.AccessoryView as UISwitch;
uiSwitch.UpdateFlowDirection(controller);
}
void UpdateIsEnabled(CellTableViewCell cell, SwitchCell switchCell)
{
cell.UserInteractionEnabled = switchCell.IsEnabled;
cell.TextLabel.Enabled = switchCell.IsEnabled;
cell.DetailTextLabel.Enabled = switchCell.IsEnabled;
var uiSwitch = cell.AccessoryView as UISwitch;
if (uiSwitch != null)
uiSwitch.Enabled = switchCell.IsEnabled;
}
}
}