来自 IB 的原型单元的参考 [英] Reference of prototype cells from IB
问题描述
我正在用 MonoTouch
中的故事板做 iOS
项目.
I am doing iOS
project with storyboards in MonoTouch
.
我有 UITableView 的自定义类 (myCustomTableFoo
) 并在 IB 中指定了此类的原型单元格.然后我想以编程方式实例化这个类.它缺少原型单元格的坏事,我不知道如何将该原型单元格插入到我以编程方式创建的表对象中.
I have the custom class of UITableView (myCustomTableFoo
) and specified the prototype cells of this class in IB. Then I want to instance this class programmatically. The bad thing it's lacking the prototype cells, and I don't know how to insert that prototype cells to my programmatically created table object.
我有一个故事板,我想在所有继承的表类中使用来自 IB
的原型单元格.我认为 MonoTouch
在这种情况下可能与 Objective-c 非常相似.
I have a storyboard and I want to use my prototype cells from IB
in all my inherited table classes. I think MonoTouch
could be pretty similar to Objective-c in this case.
推荐答案
你可以使用
this._tableView.RegisterClassForCellReuse(typeof(MyCell), new NSString("MyReuseIdentifier"));
然后你可以使用
this._tableView.DequeueReusableCell("MyReuseIdentifier");
单元格将被自动实例化.你确实需要使用 [Register("MyCell")
注册你的类,它应该有一个像
the cell would be automatically instantiated. You do need to register your class using [Register("MyCell")
and it should have a constructor like
public MyCell(IntPtr handle) : base(handle) {
}
但有一件事,我认为您不能重复使用您在故事板中定义的单元格.如果你想在不同的 TableView 实例中重用相同的单元格,你可以为你的单元格创建一个唯一的 Nib,然后类似的东西就可以了:
One thing though, I don't think you can reuse the cell you defined in your storyboard. If you want to reuse the same cells in different TableView instance, you can create a unique Nib for your cell and then something like that would do the trick:
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 时.
Here I'm using a generic view defined on an external Nib. I manually instantiate it when feeding the data into the Cell if it doesn't has been yet instantiated. It happens typically at the first time the Cell was instantiated.
这篇关于来自 IB 的原型单元的参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!