gpt4 book ai didi

ios - 来自 IB 的原型(prototype)单元引用

转载 作者:行者123 更新时间:2023-11-28 22:39:31 25 4
gpt4 key购买 nike

我正在用 MonoTouch 中的 Storyboard做 iOS 项目。

我有 UITableView 的自定义类 (myCustomTableFoo) 并在 IB 中指定了此类的原型(prototype)单元格。然后我想以编程方式实例化此类。不好的是它缺少原型(prototype)单元格,而且我不知道如何将该原型(prototype)单元格插入到我以编程方式创建的表对象中。

我有一个 Storyboard,我想在所有继承的表类中使用来自 IB 的原型(prototype)单元格。我认为 MonoTouch 在这种情况下可能与 Objective-c 非常相似。

最佳答案

你可以使用

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

然后你可以使用

this._tableView.DequeueReusableCell("MyReuseIdentifier");

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

的构造函数
public MyCell(IntPtr handle) : base(handle) {

}

但有一件事,我认为您不能重复使用您在 Storyboard 中定义的单元格。如果您想在不同的 TableView 实例中重复使用相同的单元格,您可以为您的单元格创建一个唯一的 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 上定义的通用 View 。如果尚未实例化,我会在将数据输入 Cell 时手动实例化它。它通常发生在第一次实例化 Cell 时。

关于ios - 来自 IB 的原型(prototype)单元引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14966278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com