大佬教程收集整理的这篇文章主要介绍了xamarin.ios – 如何使用Interface Builder中的自定义UITableViewCell?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我正在做的事情:
在Interface Builder中:
>我删除了“视图”并添加了一个UITableViewCell.
>将UITableViewCell的类更改为“TestCellView”.
>在单元格中添加了UILabel.
>向TestCellView添加了一个“oLblText”插座,并将UILabel连接到它.
>将类的标识符更改为“TestCellView”.
实现TestCellView.xib.cs
public partial class TestCellView : UITableViewCell { public TestCellView(string sKey) : base(UITableViewCellStyle.Default,sKey) { } public TestCellView(IntPtr oHandle) : base(oHandle) { } public string TestText { get { return this.oLblText.Text; } set { // HERE I get the null ref exception! this.oLblText.Text = value; } } }
** TestCellView.designer.cs **
[MonoTouch.Foundation.Register("TestCellView")] public partial class TestCellView { private MonoTouch.UIKit.UILabel __mt_oLblText; #pragma warning disable 0169 [MonoTouch.Foundation.Connect("oLblText")] private MonoTouch.UIKit.UILabel oLblText { get { this.__mt_oLblText = ((MonoTouch.UIKit.UILabel)(this.GetNativeField("oLblText"))); return this.__mt_oLblText; } set { this.__mt_oLblText = value; this.SetNativeField("oLblText",value); } } }
在我的表的来源:
public override UITableViewCell GetCell (UITableView tableView,NSIndexPath indexPath) { TestCellView oCell = (TestCellView)tableView.DequeueReusableCell("myCell"); if(oCell == null) { // I suppose this is wrong but how to do it correctly? // this == my UITableViewSource. NSBundle.MainBundle.LoadNib("TestCellView",this,null); oCell = new TestCellView("myCell"); } oCell.TestText = "Cell " + indexPath.Row; return oCell; }
请注意,我不想要一个涉及每个单元格的UIViewController的解决方案.我在网上看到了几个这样做的例子.我只是认为这是完全矫枉过正的.
我究竟做错了什么?
如果不是这种情况,那么您有两种选择:
>创建一个以编程方式继承UITableViewCell和您想要的每个视图的类,忘记Interface Builder.
>使用Controller添加新的iPhone视图,替换其中的视图并像对待一样对待它.除了您必须将文件连接到文件所有者中的插座这一事实.因此,当您实例化该控制器时,所有单元格的子视图都可以.
这不是一种矫枉过正,或者至少Apple推荐它:click and go to the “Loading Custom Table-View Cells From Nib Files” paragraph
PS:刚刚有类似的情况,这就是我做的方式.在MonoTouch中,对于此示例,您不必对LoadNib进行任何操作.只需在表的源代码中的GetCell覆盖内执行此操作:
using (CellController controller = new CellController()) { cell = (CustomCell)controller.View; }
甚至可能在CellController对象中声明一个额外的插座,以避免从UIView转换.
以上是大佬教程为你收集整理的xamarin.ios – 如何使用Interface Builder中的自定义UITableViewCell?全部内容,希望文章能够帮你解决xamarin.ios – 如何使用Interface Builder中的自定义UITableViewCell?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。