2011-03-19 48 views
5

我希望能夠在IB中設計自己的UITableViewCell。 但是當我嘗試訪問我在IB中定義的標籤時,我總是收到null ref異常。如何使用Interface Builder中的自定義UITableViewCell?

下面是我在做什麼:

在Interface Builder:

  • 我刪除了 「查看」,添加一個UITableViewCell代替。
  • 將UITableViewCell的類更改爲「TestCellView」。
  • 在單元格中添加了一個UILabel。
  • 增加了一個插座「oLblText」到TestCellView並將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的解決方案。我在網上看到過幾個例子。我只是認爲這是完全過度的。

我在做什麼錯?

回答

3

首先,這是一個設計問題。如果您的表格視圖始終加載具有靜態內容的特定數量的單元格(如在「設置」視圖中),請爲每個想要的行創建一個自定義單元格,並將每個單元格與一個插口連接起來。

如果不是這種情況,那麼你有兩個選擇:

  1. 創建繼承的UITableViewCell,每查看您在它要編程,忘記Interface Builder中的一類。
  2. 添加新的iPhone視圖與控制器,替換那裏的視圖,並像你一樣對待它。除了您必須將您的手機連接到文件所有者的插座之外。所以當你實例化這個控制器的時候,你所有的單元格的子視圖都可以。

這是不是矯枉過正,或至少,蘋果建議吧: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的鑄造。

+2

這正是我現在所擁有的。我只是不喜歡有一個控制器。原因是:使用默認單元格樣式時,我也沒有控制器。所以,蘋果決定不需要控制器。創建自定義單元格時,他們希望我們每個單元格都有一個控制器。 – Krumelur 2011-03-21 09:11:47

+1

是的,當您使用默認單元格樣式時,不需要任何控制器,因爲單元格是以編程方式創建的。但是您想要使用從NIB文件加載的單元,通常爲用戶界面對象。只需將一個自定義單元放在NIB文件中是不夠的。它必須以某種方式與它的文件所有者連接,在這種情況下,它必須是一個UIViewController。 – 2011-03-22 08:47:26

1

請檢查這個link。這是創建自定義TableViewCell的一篇非常有趣的文章。我認爲這個錯誤是由MonoTouch提供的xib的異步加載引起的。

您必須提供自己的costructor這樣的:

public TestCellView(string sKey) //: base(UITableViewCellStyle.Default, sKey) 
{ 
    MonoTouch.Foundation.NSBundle.MainBundle.LoadNib ("YOUR NIBNAME", this, null); 
} 

希望這有助於!

+0

根據您在此處提供的鏈接,您在上面輸入的代碼示例不正確。鏈接中的示例在**控制器**實現中調用LoadNib,而不是在單元實現中。 – 2011-03-20 19:13:55

3

在裝入Nib之前,您不能引用任何插座。有一種方法可以覆蓋,它會告訴你「您的NIB已加載,現在可以訪問這些字段」。在那之前,引用這些對象將始終返回null。

相關問題