Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
167 lines (133 sloc) 4.85 KB
using System;
using System.Drawing;
using System.Collections.Generic;
using CoreGraphics;
using Foundation;
using UIKit;
namespace RedBit.Samples.iOS.InvokeFromTestCloud
{
public partial class MasterViewController : UITableViewController
{
public static Action InvokeAddNewItem{ get; private set; }
public static Action<string> InvokeTapItem { get; private set; }
DataSource dataSource;
public MasterViewController (IntPtr handle) : base (handle)
{
InvokeAddNewItem = () => {
this.AddNewItem (null, null);
};
InvokeTapItem = (index) => {
var val = default(UInt32);
if (UInt32.TryParse (index, out val))
{
TableView.SelectRow (NSIndexPath.Create(0,val), true, UITableViewScrollPosition.Top);
this.PerformSegue("showDetail", this);
}
else
throw new Exception (string.Format ("Unable to parse index parameter of '{0}'", index));
};
Title = NSBundle.MainBundle.LocalizedString ("Master", "Master");
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad) {
PreferredContentSize = new CGSize (320f, 600f);
ClearsSelectionOnViewWillAppear = false;
}
// Custom initialization
}
public DetailViewController DetailViewController {
get;
set;
}
void AddNewItem (object sender, EventArgs args)
{
dataSource.Objects.Insert (0, DateTime.Now);
using (var indexPath = NSIndexPath.FromRowSection (0, 0))
TableView.InsertRows (new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Automatic);
}
public override void DidReceiveMemoryWarning ()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
NavigationItem.LeftBarButtonItem = EditButtonItem;
var addButton = new UIBarButtonItem (UIBarButtonSystemItem.Add, AddNewItem);
NavigationItem.RightBarButtonItem = addButton;
TableView.Source = dataSource = new DataSource (this);
}
class DataSource : UITableViewSource
{
static readonly NSString CellIdentifier = new NSString ("Cell");
readonly List<object> objects = new List<object> ();
readonly MasterViewController controller;
public DataSource (MasterViewController controller)
{
this.controller = controller;
}
public IList<object> Objects {
get { return objects; }
}
// Customize the number of sections in the table view.
public override nint NumberOfSections (UITableView tableView)
{
return 1;
}
public override nint RowsInSection (UITableView tableview, nint section)
{
return objects.Count;
}
// Customize the appearance of table view cells.
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
var cell = (UITableViewCell)tableView.DequeueReusableCell (CellIdentifier, indexPath);
cell.TextLabel.Text = objects [indexPath.Row].ToString ();
return cell;
}
public override bool CanEditRow (UITableView tableView, NSIndexPath indexPath)
{
// Return false if you do not want the specified item to be editable.
return true;
}
public override void CommitEditingStyle (UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
{
if (editingStyle == UITableViewCellEditingStyle.Delete) {
// Delete the row from the data source.
objects.RemoveAt (indexPath.Row);
controller.TableView.DeleteRows (new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Fade);
} else if (editingStyle == UITableViewCellEditingStyle.Insert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
/*
// Override to support rearranging the table view.
public override void MoveRow (UITableView tableView, NSIndexPath sourceIndexPath, NSIndexPath destinationIndexPath)
{
}
*/
/*
// Override to support conditional rearranging of the table view.
public override bool CanMoveRow (UITableView tableView, NSIndexPath indexPath)
{
// Return false if you do not want the item to be re-orderable.
return true;
}
*/
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
controller.DetailViewController.SetDetailItem (objects [indexPath.Row]);
}
}
public override void PrepareForSegue (UIStoryboardSegue segue, NSObject sender)
{
if (segue.Identifier == "showDetail") {
var indexPath = TableView.IndexPathForSelectedRow;
var item = dataSource.Objects [indexPath.Row];
((DetailViewController)segue.DestinationViewController).SetDetailItem (item);
}
}
}
}