Created
July 19, 2011 06:12
-
-
Save alexyork/1091459 to your computer and use it in GitHub Desktop.
Custom UITableViewCell's with a TableCellFactory in MonoTouch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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