我只能找到點點滴滴,沒有全面的解釋和幫助,特別是在MonoTouch中。如何刪除和重新排序UITableView中的行?
我有一個UITableView,我有我的DataSource對象,我有我的自定義CellController。我有自定義單元格上的按鈕,我想允許用戶刪除行或在列表中向上或向下移動它。
我只是不知道我應該如何管理修改數據源並重新綁定(我想用.NET術語來說,我認爲這是問題的一部分)。
謝謝。
我只能找到點點滴滴,沒有全面的解釋和幫助,特別是在MonoTouch中。如何刪除和重新排序UITableView中的行?
我有一個UITableView,我有我的DataSource對象,我有我的自定義CellController。我有自定義單元格上的按鈕,我想允許用戶刪除行或在列表中向上或向下移動它。
我只是不知道我應該如何管理修改數據源並重新綁定(我想用.NET術語來說,我認爲這是問題的一部分)。
謝謝。
我不熟悉單點觸摸,但在Objective-C API中,如果更新了數據源,則必須調用表的重裝方法。
下面是如何添加刪除到表中,我不知道重新排序。在界面構建器中,將NavigationController添加到窗口中,然後在其中添加一個TableViewController,並在屬性窗口中將其類設置爲MyTableViewController
。
我想我從教程中得到了這個,那裏有很多的演示。如果找不到值得瀏覽的示例,請點擊Miguel De Icaza's Github samples。
亞歷克斯紐約後也可能會有所幫助,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);
}
}
}