Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use custom UITableViewCell from Interface Builder?
    text
    copied!<p>I want to be able to design my own UITableViewCell in IB. But I keep getting a null ref exception when trying to access the label I defined in IB.</p> <p>Here's what I'm doing:</p> <p><strong>In Interface Builder:</strong></p> <ul> <li>I removed the "View" and added a UITableViewCell instead.</li> <li>Changed the class of the UITableViewCell to "<code>TestCellView</code>".</li> <li>Added a UILabel to the cell.</li> <li>Added an outlet "<code>oLblText</code>" to <code>TestCellView</code> and connected the UILabel to it.</li> <li>Changed the identifier of the class to "TestCellView".</li> </ul> <p><strong>Implement TestCellView.xib.cs</strong> </p> <pre class="lang-cs prettyprint-override"><code>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; } } } </code></pre> <p>** The TestCellView.designer.cs**</p> <pre class="lang-cs prettyprint-override"><code>[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); } } } </code></pre> <p><strong>In my table's source:</strong></p> <pre class="lang-cs prettyprint-override"><code>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; } </code></pre> <p>Please note that I do NOT want a solution that involves a UIViewController for every cell. I have seen a couple of examples on the web doing this. I just think it is total overkill.</p> <p>What am I doing wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload