views:

863

answers:

2

I'm only able to find bits and pieces, and no overall explanation and help, particularly in MonoTouch for this.

I have a UITableView, I have my DataSource object, and I have my custom CellController. I have buttons on the custom cells which I want to allow the user to delete the row or move it up or down in the list presented.

I just cannot figure out how I'm supposed to manage modifying the datasource and getting it to rebind (I'm thinking in .NET terms, which I think is part of the issue).

thanks.

+1  A: 

I'm not familiar with monotouch but in the Objective-C API, you have to call the table's reload method if you've updated a datasource.

TechZen
+1  A: 

Here's how you can add deleting to the table, I'm not sure about re-ordering. In interface builder, add NavigationController to your window, and then add a TableViewController inside it and set its class in the properties window to MyTableViewController.

I think I got this from the tutorials, there's a lot of demos there. If you can't find an example you're after it's worth browsing Miguel De Icaza's Github samples.

Alex York's post might also help, Deleting cells from a UITableView

[MonoTouch.Foundation.Register("MyTableViewController")]
public class MyTableViewController : UITableViewController
{
    public List<string> Items { get;set; }

    private UIBarButtonItem _buttonEdit;
    private UIBarButtonItem _buttonDone;

    public MyTableViewController(IntPtr ptr) : base(ptr)
    {   
        _buttonEdit = new UIBarButtonItem(UIBarButtonSystemItem.Edit);
        _buttonDone = new UIBarButtonItem(UIBarButtonSystemItem.Done);
        _buttonEdit.Clicked += Handle_buttonEditClicked;
        _buttonDone.Clicked += Handle_buttonDoneClicked;

        NavigationItem.RightBarButtonItem = _buttonEdit;
    }

    void Handle_buttonDoneClicked (object sender, EventArgs e)
    {
        Editing = false;
        NavigationItem.RightBarButtonItem = _buttonEdit;
    }

    void Handle_buttonEditClicked (object sender, EventArgs e)
    {
        Editing = true;
        NavigationItem.RightBarButtonItem = _buttonDone;
    }

    private void BindData()
    {
        Title = "Testing tables";
        Items = new List<string>() { "hello","world","big","bad","world"};
    }

    public override void ViewDidLoad ()
    {   
        base.ViewDidLoad ();    

        BindData();
        TableView.Delegate = new MyTableViewDelegate(this);
        TableView.DataSource = new MyTableDataSource(this);
    }

    public class MyTableDataSource : UITableViewDataSource
    {
        private MyTableViewController _tableViewController;

        public MyTableDataSource(MyTableViewController controller)
        {
            _tableViewController = controller;
        }

        public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
        {
            string cellid = "cellid"; 
            UITableViewCell cell = tableView.DequeueReusableCell(cellid); 

            if ( cell == null )
            {
                cell = new UITableViewCell(UITableViewCellStyle.Default, cellid);
                cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
            }

            cell.TextLabel.Text = _tableViewController.Items[indexPath.Row];

            return cell; 
        }

        public override void CommitEditingStyle (UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
        {
            if (editingStyle == UITableViewCellEditingStyle.Delete)
            {
                _tableViewController.Items.RemoveAt(indexPath.Row);
                tableView.DeleteRows(new [] { indexPath }, UITableViewRowAnimation.Fade);
            }
        }


        public override int RowsInSection (UITableView tableview, int section)
        {
            return _tableViewController.Items.Count;
        }

        public override string TitleForHeader (UITableView tableView, int section)
        {
            return "title";
        }
    }


    public class MyTableViewDelegate : UITableViewDelegate
    {
        private DetailsViewController _detailsViewController;
        private MyTableViewController _tableViewController;

        public MyTableViewDelegate(MyTableViewController controller)
        {
            _tableViewController = controller;  
        }

        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            if (_detailsViewController == null)
                _detailsViewController = new DetailsViewController();

            _detailsViewController.CurrentItem = _tableViewController.Items[indexPath.Row];
            _tableViewController.NavigationController.PushViewController(_detailsViewController,true);
        }
    }
}
Chris S