Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
InlineTableViewSource
/*
Snippet Name: InlineTableViewSource
Platform: iOS
Function: A subclass of UITableViewSource that allows you to define UITableViewDataSource and UITableViewDelegate methods inline, rather than subclassing. Lets you take advantage of closures and use tableviews where you can't create subclasses, like in Xamarin Sketches (compile and reference).
Funcs/Actions are prefixed with "_", feel free to alter if this disagrees with your styling guidelines.
Usage:
var cellId = new NSString("cell");
var tableView = new UITableView(View.Frame, UITableViewStyle.Grouped) {
Source = new InlineTableViewSource {
_NumberOfSections = (tv) => 2,
_RowsInSection = (tv, section) => 5,
_TitleForHeader = (tv, section) => String.Format("Section {0}", section),
_GetCell = (tv, indexPath) => {
var cell = tv.DequeueReusableCell(cellId) ?? new UITableViewCell(UITableViewCellStyle.Default, cellId);
cell.TextLabel.Text = "hi";
return cell;
}
}
};
Snippet:
*/
public class InlineTableViewSource : UITableViewSource
{
public Action<UITableView, NSIndexPath> _AccessoryButtonTapped { get; set; }
public Func<UITableView, NSIndexPath, UITableViewCellAccessory> _AccessoryForRow { get; set; }
public Func<UITableView, NSIndexPath, Boolean> _CanEditRow { get; set; }
public Func<UITableView, NSIndexPath, Boolean> _CanMoveRow { get; set; }
public Func<UITableView, NSIndexPath, UITableViewCellEditingStyle> _EditingStyleForRow { get; set; }
public Action<UITableView, UITableViewCellEditingStyle, NSIndexPath> _CommitEditingStyle { get; set; }
public Action<UITableView, NSIndexPath> _DidEndEditing { get; set; }
public Func<UITableView, nint> _NumberOfSections { get; set; }
public Func<UITableView, nint, nint> _RowsInSection { get; set; }
public Func<UITableView, NSIndexPath, UITableViewCell> _GetCell { get; set; }
public Func<UITableView, NSIndexPath, nfloat> _GetHeightForRow { get; set; }
public Func<UITableView, NSIndexPath, nint> _IndentationLevel { get; set; }
public Func<UITableView, NSIndexPath, NSIndexPath> _WillSelectRow { get; set; }
public Action<UITableView, NSIndexPath> _RowDeselected { get; set; }
public Action<UITableView, NSIndexPath> _RowSelected { get; set; }
public Func<UITableView, NSIndexPath, Boolean> _ShouldHighlightRow { get; set; }
public Func<UITableView, NSIndexPath, Boolean> _ShouldIndentWhileEditing { get; set; }
public Func<UITableView, NSIndexPath, String> _TitleForDeleteConfirmation { get; set; }
public Func<UITableView, nint, String> _TitleForFooter { get; set; }
public Func<UITableView, nint, String> _TitleForHeader { get; set; }
public override void AccessoryButtonTapped(UITableView tableView, NSIndexPath indexPath)
{
if (_AccessoryButtonTapped != null)
_AccessoryButtonTapped(tableView, indexPath);
}
public override UITableViewCellAccessory AccessoryForRow(UITableView tableView, NSIndexPath indexPath)
{
if (_AccessoryForRow != null)
return _AccessoryForRow(tableView, indexPath);
else
return UITableViewCellAccessory.None;
}
public override Boolean CanEditRow(UITableView tableView, NSIndexPath indexPath)
{
if (_CanEditRow != null)
return _CanEditRow(tableView, indexPath);
else
return false;
}
public override Boolean CanMoveRow(UITableView tableView, NSIndexPath indexPath)
{
if (_CanMoveRow != null)
return _CanMoveRow(tableView, indexPath);
else
return false;
}
public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
{
if (_CommitEditingStyle != null)
_CommitEditingStyle(tableView, editingStyle, indexPath);
}
public override void DidEndEditing(UITableView tableView, NSIndexPath indexPath)
{
if (_DidEndEditing != null)
_DidEndEditing(tableView, indexPath);
}
public override UITableViewCellEditingStyle EditingStyleForRow(UITableView tableView, NSIndexPath indexPath)
{
if (_EditingStyleForRow != null)
return _EditingStyleForRow(tableView, indexPath);
else
return UITableViewCellEditingStyle.None;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
if (_GetCell != null)
return _GetCell(tableView, indexPath);
else
throw new NotImplementedException("GetCell");
}
public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
{
if (_GetHeightForRow != null)
return _GetHeightForRow(tableView, indexPath);
else
return 44f;
}
public override nint IndentationLevel(UITableView tableView, NSIndexPath indexPath)
{
if (_IndentationLevel != null)
return _IndentationLevel(tableView, indexPath);
else
return 0;
}
public override nint NumberOfSections(UITableView tableView)
{
if (_NumberOfSections != null)
return _NumberOfSections(tableView);
else
return 1;
}
public override void RowDeselected(UITableView tableView, NSIndexPath indexPath)
{
if (_RowDeselected != null)
_RowDeselected(tableView, indexPath);
}
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
if (_RowSelected != null)
_RowSelected(tableView, indexPath);
}
public override nint RowsInSection(UITableView tableview, nint section)
{
if (_RowsInSection != null)
return _RowsInSection(tableview, section);
else
throw new NotImplementedException("RowsInSection");
}
public override Boolean ShouldHighlightRow(UITableView tableView, NSIndexPath rowIndexPath)
{
if (_ShouldHighlightRow != null)
return _ShouldHighlightRow(tableView, rowIndexPath);
else
return true;
}
public override Boolean ShouldIndentWhileEditing(UITableView tableView, NSIndexPath indexPath)
{
if (_ShouldIndentWhileEditing != null)
return _ShouldIndentWhileEditing(tableView, indexPath);
else
return true;
}
public override String TitleForDeleteConfirmation(UITableView tableView, NSIndexPath indexPath)
{
if (_TitleForDeleteConfirmation != null)
return _TitleForDeleteConfirmation(tableView, indexPath);
else
return "Delete";
}
public override String TitleForFooter(UITableView tableView, nint section)
{
if (_TitleForFooter != null)
return _TitleForFooter(tableView, section);
else
return null;
}
public override String TitleForHeader(UITableView tableView, nint section)
{
if (_TitleForHeader != null)
return _TitleForHeader(tableView, section);
else
return null;
}
public override NSIndexPath WillSelectRow(UITableView tableView, NSIndexPath indexPath)
{
if (_WillSelectRow != null)
return _WillSelectRow(tableView, indexPath);
else
return indexPath;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.