Skip to content

Instantly share code, notes, and snippets.

@wcoder
Forked from jamesmontemagno/IRemove.cs
Last active February 27, 2016 20:15
Show Gist options
  • Save wcoder/754e647be531711be5ab to your computer and use it in GitHub Desktop.
Save wcoder/754e647be531711be5ab 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment