IB的原型单元的参考

我正在做与MonoTouch故事板的iOS项目。

我有自定义类的UITableView( myCustomTableFoo ),并在IB中指定此类的原型单元格。 然后我想以编程方式实例化这个类。 坏的是它缺less原型单元格,我不知道如何将原型单元格插入到我的程序创build的表格对象中。

我有一个故事板,我想在我的所有inheritance的表类中使用IB原型单元格。 在这种情况下,我认为MonoTouch可以和Objective-c非常相似。

你可以使用

 this._tableView.RegisterClassForCellReuse(typeof(MyCell), new NSString("MyReuseIdentifier")); 

那么你可以使用你的细胞出列

 this._tableView.DequeueReusableCell("MyReuseIdentifier"); 

该单元格将被自动实例化。 您需要使用[Register("MyCell")注册您的类,它应该有一个构造函数

 public MyCell(IntPtr handle) : base(handle) { } 

有一件事,我不认为你可以重用你在故事板中定义的单元格。 如果你想在不同的TableView实例中重复使用相同的单元格,你可以为你的单元格创build一个独特的Nib,然后类似的东西就可以实现:

 public partial class MyCell : UITableViewCell { private MySuperCellView _mySuperNibView; public MyCell (IntPtr handle) : base (handle) { } private void checkState() { // Check if this is the first time the cell is instantiated if (this._mySuperNibView == null) { // It is, so create its view NSArray array = NSBundle.MainBundle.LoadNib("NibFileName", this, null); this._mySuperNibView = (MySuperCellView)Runtime.GetNSObject(array.ValueAt(0)); this._mySuperNibView.Frame = this.Bounds; this._mySuperNibView.LayoutSubviews(); this.AddSubview(this._mySuperNibView); } } public object cellData { get { return this._mySuperNibView.cellData; } set { this.checkState(); this._mySuperNibView.cellData = value; } } } 

这里我使用了在外部Nib上定义的通用视图。 如果数据没有被实例化,则在将数据提供给Cell时手动实例化它。 它通常在Cell初次实例化时发生。