1

我只能找到零碎的东西,并没有全面的解释和帮助,尤其是在 MonoTouch 中。

我有一个 UITableView,我有我的 DataSource 对象,我有我的自定义 CellController。我在自定义单元格上有按钮,我希望允许用户在显示的列表中删除该行或将其向上或向下移动。

我只是无法弄清楚我应该如何管理修改数据源并让它重新绑定(我正在考虑 .NET 术语,我认为这是问题的一部分)。

谢谢。

4

2 回答 2

7

以下是如何将删除添加到表中,我不确定重新排序。在界面生成器中,将 NavigationController 添加到您的窗口,然后在其中添加一个 TableViewController 并将其在属性窗口中的类设置为MyTableViewController.

我想我是从教程中得到的,那里有很多演示。如果您找不到示例,则值得浏览Miguel De Icaza 的 Github 示例

Alex York 的帖子也可能有所帮助,从 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);
        }
    }
}
于 2010-02-06T15:30:52.967 回答
2

我不熟悉 monotouch,但在 Objective-C API 中,如果您更新了数据源,则必须调用表的 reload 方法。

于 2010-01-30T05:14:33.740 回答