Skip to content

Instantly share code, notes, and snippets.

@alexyork
Created July 19, 2011 06:12
Show Gist options
  • Save alexyork/1091459 to your computer and use it in GitHub Desktop.
Save alexyork/1091459 to your computer and use it in GitHub Desktop.
Custom UITableViewCell's with a TableCellFactory in MonoTouch
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.ObjCRuntime;
namespace CustomTableCellApp
{
public class TableCellFactory<T> where T : UITableViewCell
{
private string cellId;
private string nibName;
private NSObject cellTemplate;
public TableCellFactory(string cellId, string nibName)
{
this.cellId = cellId;
this.nibName = nibName;
// Load the cell template from the XIB - once and only once!
var cell = Activator.CreateInstance<T>();
var views = NSBundle.MainBundle.LoadNib(nibName, cell, null);
this.cellTemplate = views.ValueAt(0);
}
public T GetCell(UITableView tableView)
{
var cell = tableView.DequeueReusableCell(cellId) as T;
if (cell == null)
{
cell = Runtime.GetNSObject( cellTemplate ) as T;
}
return cell;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment