-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathTableViewCellDemo.cs
More file actions
202 lines (178 loc) · 4.63 KB
/
TableViewCellDemo.cs
File metadata and controls
202 lines (178 loc) · 4.63 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using CoreGraphics;
using Foundation;
using UIKit;
using XibFree;
using System.Collections.Generic;
namespace Demo
{
public partial class TableViewCellDemo : UITableViewController
{
public TableViewCellDemo() : base(UITableViewStyle.Grouped)
{
this.Title = "TableViewCell";
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Create some items
var r = new Random();
for (int i=0; i<100; i++)
{
var item = new Item();
item.Title = string.Format("Item {0}", i+1);
item.Total = r.Next(999)+1;
item.Count = r.Next(item.Total);
_items.Add(item);
}
this.TableView.Source = new Source(this);
this.TableView.RowHeight = new DemoTableViewCell().MeasureHeight();
}
class Item
{
public string Title;
public int Count;
public int Total;
public int Percentage
{
get
{
return Count * 100 / Total;
}
}
};
List<Item> _items = new List<Item>();
class DemoTableViewCell : UITableViewCell
{
public DemoTableViewCell() : base(UITableViewCellStyle.Default, "DemoTableViewCell")
{
_layout = new LinearLayout(Orientation.Horizontal)
{
Padding = new UIEdgeInsets(5,5,5,5),
LayoutParameters = new LayoutParameters()
{
Width = AutoSize.FillParent,
Height = AutoSize.WrapContent,
},
SubViews = new View[]
{
new NativeView()
{
View = new UIImageView(CGRect.Empty)
{
Image = UIImage.FromBundle("tts512.png"),
},
LayoutParameters = new LayoutParameters()
{
Width = 40,
Height = 40,
Margins = new UIEdgeInsets(0,0,0,10),
}
},
new LinearLayout(Orientation.Vertical)
{
LayoutParameters = new LayoutParameters()
{
Width = AutoSize.FillParent,
Height = AutoSize.WrapContent,
},
SubViews = new View[]
{
new NativeView()
{
View = _labelTitle = new UILabel()
{
BackgroundColor = UIColor.Clear,
Font = UIFont.BoldSystemFontOfSize(18),
HighlightedTextColor = UIColor.White,
},
LayoutParameters = new LayoutParameters()
{
Width = AutoSize.FillParent,
Height = AutoSize.WrapContent,
}
},
new NativeView()
{
View = _labelSubTitle = new UILabel()
{
BackgroundColor = UIColor.Clear,
Font = UIFont.SystemFontOfSize(12),
TextColor = UIColor.DarkGray,
HighlightedTextColor = UIColor.White,
},
LayoutParameters = new LayoutParameters()
{
Width = AutoSize.FillParent,
Height = AutoSize.WrapContent,
}
},
}
},
new NativeView()
{
View = _labelPercent = new UILabel()
{
BackgroundColor = UIColor.Clear,
TextColor = UIColor.FromRGB(51,102,153),
HighlightedTextColor = UIColor.White,
Font = UIFont.BoldSystemFontOfSize(20),
TextAlignment = UITextAlignment.Right,
},
LayoutParameters = new LayoutParameters()
{
Width = 50,
Height = AutoSize.FillParent,
Margins = new UIEdgeInsets(0, 10, 0, 0),
}
}
}
};
this.ContentView.Add(new UILayoutHost(_layout, this.ContentView.Bounds));
this.Accessory = UITableViewCellAccessory.DisclosureIndicator;
}
public void Init(Item i)
{
_labelTitle.Text = i.Title;
_labelSubTitle.Text = string.Format("{0} of {1}", i.Count, i.Total);
_labelPercent.Text = string.Format("{0}%", i.Percentage);
}
// Helper to get the required height for a cell
public nfloat MeasureHeight()
{
// Get the layout to measure itself, and then retrieve the measured height
_layout.Measure(nfloat.MaxValue, nfloat.MaxValue);
return _layout.GetMeasuredSize().Height;
}
ViewGroup _layout;
UILabel _labelTitle;
UILabel _labelSubTitle;
UILabel _labelPercent;
}
class Source : UITableViewSource
{
public Source(TableViewCellDemo owner)
{
_owner = owner;
}
TableViewCellDemo _owner;
#region implemented abstract members of UITableViewSource
public override nint RowsInSection(UITableView tableview, nint section)
{
return _owner._items.Count;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var cell = (DemoTableViewCell)tableView.DequeueReusableCell("DemoTableViewCell");
if (cell==null)
{
cell = new DemoTableViewCell();
}
var item = _owner._items[indexPath.Row];
cell.Init(item);
return cell;
}
#endregion
}
}
}