Skip to content

Instantly share code, notes, and snippets.

@jfoshee
Created March 22, 2013 19:20
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfoshee/5223962 to your computer and use it in GitHub Desktop.
Save jfoshee/5223962 to your computer and use it in GitHub Desktop.
Implementation of MonoTouch (Xamarin.iOS) UIPickerViewModel for an IList<T>.
namespace Unplugged
{
public abstract class ListPickerViewModel<TItem> : UIPickerViewModel
{
public TItem SelectedItem { get; private set; }
IList<TItem> _items;
public IList<TItem> Items
{
get { return _items; }
set { _items = value; Selected(null, 0, 0); }
}
public ListPickerViewModel()
{
}
public ListPickerViewModel(IList<TItem> items)
{
Items = items;
}
public override int GetRowsInComponent(UIPickerView picker, int component)
{
if (NoItem())
return 1;
return Items.Count;
}
public override string GetTitle(UIPickerView picker, int row, int component)
{
if (NoItem(row))
return "";
var item = Items[row];
return GetTitleForItem(item);
}
public override void Selected(UIPickerView picker, int row, int component)
{
if (NoItem(row))
SelectedItem = default(TItem);
else
SelectedItem = Items[row];
}
public override int GetComponentCount(UIPickerView picker)
{
return 1;
}
public virtual string GetTitleForItem(TItem item)
{
return item.ToString();
}
bool NoItem(int row = 0)
{
return Items == null || row >= Items.Count;
}
}
}
@jsonmez
Copy link

jsonmez commented Jul 31, 2013

Awesome, handy snippet. Can I use this in my Pluralsight course?

@jfoshee
Copy link
Author

jfoshee commented May 21, 2014

Sure, @jsonmez. (10 months later)

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