This repository was archived by the owner on Jul 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 610
Expand file tree
/
Copy pathTableViewController.cs
More file actions
89 lines (72 loc) · 3.14 KB
/
TableViewController.cs
File metadata and controls
89 lines (72 loc) · 3.14 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
using System;
using UIKit;
using CoreGraphics;
using Foundation;
namespace CodeHub.iOS.ViewControllers
{
public class TableViewController : BaseViewController
{
private readonly Lazy<UITableView> _tableView;
private UIRefreshControl _refreshControl;
public UITableView TableView => _tableView.Value;
public bool ClearSelectionOnAppear { get; set; } = true;
public virtual UIRefreshControl RefreshControl
{
get { return _refreshControl; }
set
{
_refreshControl?.RemoveFromSuperview();
_refreshControl = value;
if (_refreshControl != null)
TableView.AddSubview(_refreshControl);
}
}
public TableViewController(UITableViewStyle style)
{
_tableView = new Lazy<UITableView>(() => new UITableView(CGRect.Empty, style));
NavigationItem.BackBarButtonItem = new UIBarButtonItem { Title = string.Empty };
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
TableView.Frame = View.Bounds;
TableView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleTopMargin;
TableView.AutosizesSubviews = true;
TableView.CellLayoutMarginsFollowReadableWidth = false;
TableView.EstimatedSectionFooterHeight = 0;
TableView.EstimatedSectionHeaderHeight = 0;
Add(TableView);
}
NSObject _hideNotification, _showNotification;
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
_hideNotification = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, OnKeyboardHideNotification);
_showNotification = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, OnKeyboardNotification);
var index = TableView.IndexPathForSelectedRow;
if (ClearSelectionOnAppear && index != null)
TableView.DeselectRow(index, true);
}
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
View.EndEditing(true);
if (_hideNotification != null)
NSNotificationCenter.DefaultCenter.RemoveObserver(_hideNotification);
if (_showNotification != null)
NSNotificationCenter.DefaultCenter.RemoveObserver(_showNotification);
}
private void OnKeyboardHideNotification(NSNotification notification)
{
TableView.ContentInset = UIEdgeInsets.Zero;
TableView.ScrollIndicatorInsets = UIEdgeInsets.Zero;
}
private void OnKeyboardNotification (NSNotification notification)
{
var keyboardFrame = UIKeyboard.FrameEndFromNotification (notification);
var inset = new UIEdgeInsets(0, 0, keyboardFrame.Height, 0);
TableView.ContentInset = inset;
TableView.ScrollIndicatorInsets = inset;
}
}
}