-
Notifications
You must be signed in to change notification settings - Fork 3
/
SessionsViewController.cs
138 lines (122 loc) · 4.4 KB
/
SessionsViewController.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace Monospace2
{
/// <summary>
/// First view that users see - lists the top level of the hierarchy xml
/// </summary>
/// <remarks>
/// LOADS data from the xml files into public properties (deserialization)
/// then we pass around references to the MainViewController so other
/// ViewControllers can access the data.</remarks>
[Register]
public class SessionsViewController : UIViewController
{
private UITableView tableView;
private Database _database;
private List<Session> _sessions;
private string _date;
public SessionsViewController(string date)
{
_date = date;
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
_database = AppDelegate.SessionDatabase;
_sessions = _database.GetSessions(_date).ToList();
// no XIB !
tableView = new UITableView()
{
Delegate = new TableViewDelegate(this, _date, _sessions),
DataSource = new TableViewDataSource(this, _date, _sessions),
AutoresizingMask = UIViewAutoresizing.FlexibleHeight|
UIViewAutoresizing.FlexibleWidth,
BackgroundColor = UIColor.White,
};
// Set the table view to fit the width of the app.
tableView.SizeToFit();
// Reposition and resize the receiver
tableView.Frame = new RectangleF (0, 0, this.View.Frame.Width, this.View.Frame.Height);
// Add the table view as a subview
this.View.AddSubview(tableView);
}
private class TableViewDelegate : UITableViewDelegate
{
private SessionsViewController _svc;
private string _date;
private List<Session> _sessions;
public TableViewDelegate(SessionsViewController controller, string date, List<Session> sessions)
{
_svc = controller;
_date = date;
_sessions = sessions;
}
/// <summary>
/// If there are subsections in the hierarchy, navigate to those
/// ASSUMES there are _never_ Categories hanging off the root in the hierarchy
/// </summary>
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
Session s = _sessions[indexPath.Row];
Console.WriteLine("SessionsViewController:TableViewDelegate.RowSelected: Label=");
SessionViewController sessionView = new SessionViewController(s);
sessionView.Title = s.Title;
_svc.NavigationController.PushViewController(sessionView, true);
}
}
private class TableViewDataSource : UITableViewDataSource
{
static NSString kCellIdentifier = new NSString ("MySessionIdentifier");
private SessionsViewController _dvc;
private List<Session> _sessions;
private Dictionary<int, SessionCellController> controllers = null;
public TableViewDataSource (SessionsViewController controller, string date, List<Session> sessions)
{
_dvc = controller;
_sessions = sessions;
controllers = new Dictionary<int, SessionCellController>();
}
public override int RowsInSection (UITableView tableview, int section)
{
return _sessions.Count;
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell (kCellIdentifier);
Session s = _sessions[indexPath.Row];
SessionCellController cellController = null;
if (cell == null)
{
Console.WriteLine("no cell");
cellController = new SessionCellController();
NSBundle.MainBundle.LoadNib("SessionCellController", cellController, null);
cell = cellController.Cell;
cell.Tag = Environment.TickCount;
controllers.Add(cell.Tag, cellController);
}
else
{
Console.WriteLine("reuse cell");
cellController = controllers[cell.Tag];
}
Console.WriteLine("setting disclosure");
cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
Console.WriteLine("loading data");
try
{
cellController.SessionTitle = s.Title;
cellController.Subtitle = s.Location;
cellController.Time = s.StartTime;
cellController.EndTime = s.EndTime;
} catch (Exception){}
return cell;
}
}
}
}