简体   繁体   中英

UITableView Multiselect in MonoTouch

I am having trouble figuring out how to implement the following style multitouch UITableView with Monotouch: 在此处输入图片说明

I have the UITableView working with the 'slide to delete' functionality. I've also added the following:

logsTable.AllowsMultipleSelection = true;
logsTable.AllowsMultipleSelectionDuringEditing = true;

Which allows me to select the rows, however the circles and ticks do not appear. Is this a default iOS feature or do I have to implement it separately?

You need to create a class that inherits from UITableViewSource . You need to implement at least two methods in this newly created: GetCell and RowsInSection . You can use a custom UITableViewCell to create the exact design of the cell or you can use the Accessory property to modify a cell accessory when the cell was selected. You should have something similar to this:

private class SimpleTableViewSource : UITableViewSource
{
   // Some data. Item1 will serve as the Text and Item2 will be a value indicating whether the cell is selected or not
   private List<Tuple<string, bool>> Data { get; set; }

   public SimpleTableViewSource()
   {
       this.Data = new List<Tuple<string, bool>>() {
           Tuple.Create("Item 1", false),
           Tuple.Create("Item 2", false),
           Tuple.Create("Item 3", false),
           Tuple.Create("Item 4", false),
           Tuple.Create("Item 5", false),
           Tuple.Create("Item 6", false),
           Tuple.Create("Item 7", false)
       };
   }

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

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

       cell.TextLabel.Text = this.Data[indexPath.Row].Item1;

       // if the row is selected show checkmark
       if (this.Data[indexPath.Row].Item2)
       {
           cell.Accessory = UITableViewCellAccessory.Checkmark;
       }
       else
       {
           cell.Accessory = UITableViewCellAccessory.None;
       }

       return cell;
   }

   public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
   {
       UITableViewCell cell = tableView.DequeueReusableCell("cell") ?? new UITableViewCell();
       cell.Selected = !this.Data[indexPath.Row].Item2;
       this.Data[indexPath.Row] = Tuple.Create(this.Data[indexPath.Row].Item1, !this.Data[indexPath.Row].Item2);
       tableView.ReloadData();
   }    
}

And to set up your instance of UITableView (eg in ViewDidLoad ):

this.tableView.Source = new SimpleTableViewSource();
this.tableView.ReloadData();

you only need add :

logsTable.SetEditing (true, true);

or to hide

logsTable.SetEditing (false, true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM