2013-03-03 29 views
1

從我的CustomClass:UICollectionViewCell訪問插座時,它們顯示爲未初始化並且無法設置適當的值。無法使用帶有UICollectionViewCell的自定義XIB插座

我見過的每個例子都使用普通的類(無​​XIB)來設置UI。

[Register("CustomCommentCell")] 
public partial class CustomCommentCell : UICollectionViewCell 
{ 
    public static readonly NSString Identifier = new NSString("CustomCommentCell"); 

    public CustomCommentCell() : base() 
    { 
    } 

    public CustomCommentCell (IntPtr handle) : base (handle) 
    { 
    } 

    public void updateData() 
    { 
     this.lblComment.Text = "Test"; 
    } 
} 

在另一方面,我已經註冊類: this.tableComments.RegisterClassForCell(typeof運算(CustomCommentCell),commentCellId);

並正確設置GetCell。 但是,試圖將出口設置爲特定值時,表示它爲空。 (this.lblcomment = null),而它應該是一個UILabel初始化的。

任何線索?

回答

0

我無法遵循你所看到的很多問題。什麼是「自定義XIB插座」?爲什麼這個問題被標記爲「自定義控件」?是否有一些示例代碼或圖片可以幫助解釋問題?


我用UICollectionViewCell的的方法是一樣的,因爲我使用的UITableViewCell - 參見教程 - http://slodge.blogspot.co.uk/2013/01/uitableviewcell-using-xib-editor.html


更新:從您發佈的評論的代碼(不確定它是否完整),我認爲這對你遵循這個教程很有用。有幾個步驟可以完成,包括註冊自定義類名並使用RegisterNibForCellReuse - 其中一個可能會爲您解決這個問題。

+0

上面我已經更新了代碼的片段。 – user2084102 2013-03-04 18:09:10

+0

編輯您的問題並在其中粘貼完整的代碼 - 我無法在評論中閱讀該代碼。 – Stuart 2013-03-04 18:11:23

+0

我已經添加了this.tableComments.RegisterClassForCell。 \t \t \t this.tableComments.RegisterClassForCell(typeof(CustomCommentCell),commentCellId); – user2084102 2013-03-04 18:23:11

2

使用XIB創建Custom CollectionViewCell。執行以下操作

1)創建從UIcollectionViewCell

[Register("MyCustomCell")] 
public class MyCustomCell : UICollectionViewCell 
{ 

    public static readonly NSString Key = new NSString ("MyCustomCell"); 

    [Export ("initWithFrame:")] 
    public MyCustomCell(CoreGraphics.CGRect frame) : base (frame) 
    { 



    } 

    public override UIView ContentView { 
     get { 
      var arr= NSBundle.MainBundle.LoadNib ("MyCustomCell", this, null); 
      UIView view =arr.GetItem<UIView> (0); 
      view.Frame = base.ContentView.Frame; 
      base.ContentView.AddSubview (view); 
      return base.ContentView; 
     } 
    } 

} 

2繼承C#類)添加IphoneView XIB文件具有相同的名稱與在步驟創建的類的1

3)打開XIB在Xcode和做以下更改

enter image description here

3.1)選擇FileOwner設置類相同的名稱步驟1 enter image description here 3.2)選擇視圖設置類名稱的UIView

4)設計你的XIB因此