149 строки
4.2 KiB
C#
149 строки
4.2 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Collections.Generic;
|
|
|
|
using CoreGraphics;
|
|
using Foundation;
|
|
using UIKit;
|
|
|
|
namespace MyMasterDetailApp
|
|
{
|
|
public partial class MasterViewController : UITableViewController
|
|
{
|
|
DataSource dataSource;
|
|
|
|
public MasterViewController (IntPtr handle) : base (handle)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|