Created
April 7, 2014 16:57
-
-
Save praeclarum/10024108 to your computer and use it in GitHub Desktop.
This is my UITableViewController with DataSource property that listens for INotifyCollectionChanged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Specialized; | |
using System.Diagnostics; | |
using MonoTouch.Foundation; | |
using MonoTouch.UIKit; | |
namespace Praeclarum.UI | |
{ | |
[Register ("ObservableTableViewController")] | |
public class ObservableTableViewController : UITableViewController | |
{ | |
public UITableViewRowAnimation AddAnimation { get; set; } | |
public UITableViewRowAnimation DeleteAnimation { get; set; } | |
object dataSource; | |
IList list; | |
INotifyCollectionChanged notifier; | |
System.Threading.Thread mainThread; | |
bool loadedView = false; | |
public object DataSource { | |
get { | |
return dataSource; | |
} | |
set { | |
if (dataSource == value) | |
return; | |
if (notifier != null) { | |
notifier.CollectionChanged -= HandleCollectionChanged; | |
} | |
dataSource = value; | |
list = value as IList; | |
notifier = value as INotifyCollectionChanged; | |
if (notifier != null) { | |
notifier.CollectionChanged += HandleCollectionChanged; | |
} | |
if (loadedView) { | |
TableView.ReloadData (); | |
} | |
} | |
} | |
public ObservableTableViewController () | |
: base (UITableViewStyle.Plain) | |
{ | |
Initialize (); | |
} | |
public ObservableTableViewController (UITableViewStyle withStyle) | |
: base (withStyle) | |
{ | |
Initialize (); | |
} | |
void Initialize () | |
{ | |
mainThread = System.Threading.Thread.CurrentThread; | |
AddAnimation = UITableViewRowAnimation.Automatic; | |
DeleteAnimation = UITableViewRowAnimation.Automatic; | |
} | |
public override void ViewDidLoad () | |
{ | |
base.ViewDidLoad (); | |
TableView.Source = CreateSource (); | |
loadedView = true; | |
} | |
protected virtual ObservableTableSource CreateSource () | |
{ | |
return new ObservableTableSource (this); | |
} | |
protected virtual UITableViewCell CreateCell (NSString reuseId) | |
{ | |
return new UITableViewCell (UITableViewCellStyle.Default, reuseId); | |
} | |
protected virtual void BindCell (UITableViewCell cell, object item, NSIndexPath indexPath) | |
{ | |
cell.TextLabel.Text = item.ToString (); | |
} | |
protected virtual void OnRowSelected (object item, NSIndexPath indexPath) | |
{ | |
} | |
void HandleCollectionChanged (object sender, NotifyCollectionChangedEventArgs e) | |
{ | |
if (!loadedView) | |
return; | |
NSAction act = () => { | |
if (e.Action == NotifyCollectionChangedAction.Reset) { | |
TableView.ReloadData (); | |
} | |
if (e.Action == NotifyCollectionChangedAction.Add) { | |
var count = e.NewItems.Count; | |
var paths = new NSIndexPath[count]; | |
for (var i = 0; i < count; i++) { | |
paths [i] = NSIndexPath.FromRowSection (e.NewStartingIndex + i, 0); | |
} | |
TableView.InsertRows (paths, AddAnimation); | |
} else if (e.Action == NotifyCollectionChangedAction.Remove) { | |
var count = e.OldItems.Count; | |
var paths = new NSIndexPath[count]; | |
for (var i = 0; i < count; i++) { | |
paths [i] = NSIndexPath.FromRowSection (e.OldStartingIndex + i, 0); | |
} | |
TableView.DeleteRows (paths, DeleteAnimation); | |
} | |
}; | |
var isMainThread = System.Threading.Thread.CurrentThread == mainThread; | |
if (isMainThread) { | |
act (); | |
} else { | |
NSOperationQueue.MainQueue.AddOperation (act); | |
NSOperationQueue.MainQueue.WaitUntilAllOperationsAreFinished (); | |
} | |
} | |
protected class ObservableTableSource : UITableViewSource | |
{ | |
readonly ObservableTableViewController controller; | |
static readonly NSString reuseId = new NSString ("C"); | |
public ObservableTableSource (ObservableTableViewController controller) | |
{ | |
this.controller = controller; | |
} | |
public override void RowSelected (UITableView tableView, NSIndexPath indexPath) | |
{ | |
var item = controller.list != null ? controller.list [indexPath.Row] : null; | |
try { | |
controller.OnRowSelected (item, indexPath); | |
} catch (Exception ex) { | |
Debug.WriteLine (ex); | |
} | |
} | |
public override int NumberOfSections (UITableView tableView) | |
{ | |
return 1; | |
} | |
public override int RowsInSection (UITableView tableview, int section) | |
{ | |
var coll = controller.list; | |
return coll != null ? coll.Count : 0; | |
} | |
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) | |
{ | |
var cell = tableView.DequeueReusableCell (reuseId) ?? | |
controller.CreateCell (reuseId); | |
try { | |
var coll = controller.list; | |
if (coll != null) { | |
var obj = coll[indexPath.Row]; | |
controller.BindCell (cell, obj, indexPath); | |
} | |
return cell; | |
} | |
catch (Exception ex) { | |
Debug.WriteLine (ex); | |
} | |
return cell; | |
} | |
} | |
} | |
} |
Hi Frank, this code is very interesting for me. I tried to integrate it into a current Xamarin project, and after fixing minor API changes I'm facing the problem, that in the current API of Xamarin, obviously the NSAction
class is missing. Do you have any hints how to get it running?
[edit] Fixed it, almost too easy: Replace NSAction
with Action
. See here. Thanks for your great work!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use this class as simply as:
Of course, to make it reactive, you will want to give it a collection that implements
INotifyCollectionChanged
. For example:To customize the cells, derive a new class and override the
CreateCell
andBindCell
methods. To get full control, overrideCreateSource
to return your own customUITableViewSource
.This class can handle notification events from non-UI threads.