Skip to content

Instantly share code, notes, and snippets.

@jamesmontemagno
Last active November 16, 2021 08:46
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save jamesmontemagno/6985403 to your computer and use it in GitHub Desktop.
Save jamesmontemagno/6985403 to your computer and use it in GitHub Desktop.
A simple and Intuitive Xamarin.iOS + MvvmCross TableView swipe to delete implementation. This will allow you to have any number of ViewModels simply implement IRemove.cs and all you need is MvxDeleteStandarTableViewSource and implement the interface in your viewmodels! I should say that these are edited classes, you should implement some of the …
public interface IRemove
{
ICommand RemoveCommand { get; }
}
public class MvxDeleteStandardTableViewSource : MvxStandardTableViewSource
{
private IRemove m_ViewModel;
#region Constructors
public MvxDeleteStandardTableViewSource(IRemove viewModel, UITableView tableView, UITableViewCellStyle style, NSString cellIdentifier, IEnumerable<MvxBindingDescription> descriptions, UITableViewCellAccessory tableViewCellAccessory = 0)
: base(tableView, style, cellIdentifier, descriptions, tableViewCellAccessory)
{
m_ViewModel = viewModel;
}
public MvxDeleteStandardTableViewSource(IRemove viewModel, UITableView tableView, string bindingText) : base(tableView, bindingText)
{
m_ViewModel = viewModel;
}
public MvxDeleteStandardTableViewSource(IRemove viewModel, UITableView tableView, NSString cellIdentifier) : base(tableView, cellIdentifier)
{
m_ViewModel = viewModel;
}
public MvxDeleteStandardTableViewSource(IRemove viewModel, UITableView tableView) : base(tableView)
{
m_ViewModel = viewModel;
}
public MvxDeleteStandardTableViewSource(IRemove viewModel, UITableView tableView, UITableViewCellStyle style, NSString cellId, string binding, UITableViewCellAccessory accessory)
: base(tableView, style, cellId, binding, accessory)
{
m_ViewModel = viewModel;
}
#endregion
public override bool CanEditRow(UITableView tableView, NSIndexPath indexPath)
{
return true;
}
public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
{
switch (editingStyle)
{
case UITableViewCellEditingStyle.Delete:
m_ViewModel.RemoveCommand.Execute(indexPath.Row);
break;
case UITableViewCellEditingStyle.None:
break;
}
}
public override UITableViewCellEditingStyle EditingStyleForRow(UITableView tableView, NSIndexPath indexPath)
{
return UITableViewCellEditingStyle.Delete;
}
public override bool CanMoveRow(UITableView tableView, NSIndexPath indexPath)
{
return false;
}
}
[Register("MyView")]
public class MyView : MvxTableViewController
{
private MyViewModel m_ViewModel;
public new MyViewModel ViewModel
{
get { return m_ViewModel ?? (m_ViewModel = base.ViewModel as MyViewModel); }
}
public MyView()
: base()
{
this.Title = "MyViewModel";
}
private UIBarButtonItem m_EditButton;
public override void ViewDidLoad()
{
base.ViewDidLoad();
var source = new MvxDeleteStandardTableViewSource (ViewModel, TableView, UITableViewCellStyle.Default, new NSString("my_item"), "TitleText Title", UITableViewCellAccessory.DetailDisclosureButton);
TableView.Source = source;
var set = this.CreateBindingSet<MyView, MyViewModel>();
set.Bind(source).To(vm => vm.List);
set.Apply ();
m_EditButton = new UIBarButtonItem (NSBundle.MainBundle.LocalizedString("Edit", "Edit"), UIBarButtonItemStyle.Done, delegate {
m_TableView.SetEditing(!m_TableView.Editing, true);
m_EditButton.Title = m_TableView.Editing ?
NSBundle.MainBundle.LocalizedString("Done", "Done") :
NSBundle.MainBundle.LocalizedString("Edit", "Edit");
});
NavigationItem.RightBarButtonItem = m_EditButton;
}
}
}
public class MyViewModel : MvxViewModel, IRemove
{
#region IRemove implementation
private MvxCommand<int> m_RemoveCommand;
private ObservableCollection<string> m_List = new ObservableCollection<string>();
public ICommand RemoveCommand
{
get { return m_RemoveCommand ?? (m_RemoveCommand = new MvxCommand<int>(i=>m_List.RemoveAt(i))); }
}
#endregion
}
@flyingxu
Copy link

This code works. Just a side note, if someone is not going to delete the item but to do something then hide the delete button, you can use tableView.SetEditing(false, true); in CommitEditingStyle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment