Skip to content

Instantly share code, notes, and snippets.

@mattleibow
Created February 2, 2015 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattleibow/162d2a18a354251fa9e9 to your computer and use it in GitHub Desktop.
Save mattleibow/162d2a18a354251fa9e9 to your computer and use it in GitHub Desktop.
Preserving SWTableViewCell UI state
using System;
using Foundation;
using UIKit;
using System.Collections.Generic;
using SWTableViewCell;
using System.Linq;
namespace TestNamespace
{
public partial class TestViewController : UITableViewController
{
public SWTVC_testViewController (IntPtr handle)
: base (handle)
{
}
ViewDataSource dataSource;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
NavigationItem.RightBarButtonItem = new UIBarButtonItem (UIBarButtonSystemItem.Refresh, delegate {
TableView.ReloadData ();
});
dataSource = new ViewDataSource ();
TableView.DataSource = dataSource;
}
}
// the data that we want in the table
public class ViewDataSource : UITableViewDataSource
{
readonly List<CellViewModel> testArray;
public ViewDataSource ()
{
testArray = Enumerable
.Range (1, 100)
.Select (i => new CellViewModel(i))
.ToList ();
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
return MemoryTableViewCell.GetCell (tableView, testArray [indexPath.Row]);
}
public override nint RowsInSection (UITableView tableView, nint section)
{
return testArray.Count;
}
}
// the cell holds the logic that maps the data and state between the cell
public class MemoryTableViewCell : SWTableViewCell.SWTableViewCell, ISWTableViewCellDelegate
{
CellViewModel viewModel;
bool isPreparing;
public MemoryTableViewCell (UITableViewCellStyle style, string id)
: base (style, id)
{
}
// mark the cell as invalid
public override void PrepareForReuse ()
{
// we need to do this as this method resets the scroll offset
isPreparing = true;
base.PrepareForReuse ();
}
// when the cell item is scrolled left or right
[Export ("swipeableTableViewCell:scrollingToState:")]
public virtual void ScrollingToState (SWTableViewCell.SWTableViewCell cell, SWCellState state)
{
// only update once the cell is visible
if (!isPreparing) {
viewModel.State = state;
}
}
// logic to create and update a cell
public static MemoryTableViewCell GetCell (UITableView tableView, CellViewModel viewModel)
{
// see if there is an old cell
var cell = (MemoryTableViewCell)tableView.DequeueReusableCell ("cellIdentifier");
// create a brand new cell
if (cell == null) {
// the buttons
var rightUtilityButtons = new NSMutableArray ();
rightUtilityButtons.AddUtilityButton (UIColor.FromRGBA (0.78f, 0.78f, 0.8f, 1.0f), "More");
rightUtilityButtons.AddUtilityButton (UIColor.FromRGBA (1.0f, 0.231f, 0.188f, 1.0f), "Delete");
var buttons = NSArray.FromArray<UIButton> (rightUtilityButtons);
// the cell
cell = new MemoryTableViewCell (UITableViewCellStyle.Default, "cellIdentifier");
cell.RightUtilityButtons = buttons;
cell.WeakDelegate = cell;
}
// so that we can save the item state
cell.viewModel = viewModel;
// update the cell visuals
cell.TextLabel.Text = viewModel.Value;
// restore view state
if (viewModel.State == SWCellState.Right)
cell.ShowRightUtilityButtons (false);
cell.isPreparing = false;
return cell;
}
}
// the view model holds the data as well as any UI state
public class CellViewModel
{
public CellViewModel (int item)
{
Value = string.Format ("Item {0}...", item);
State = SWCellState.Center;
}
public string Value{ get; set; }
public SWCellState State{ get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment