1

从我的 CustomClass 访问网点时:UICollectionViewCell,它们显示为未初始化并且无法设置正确的值。

我看到的每个示例都使用普通类(无 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。但是,当尝试将 outlet 设置为特定值时,它表示它为 null。(this.lblcomment = null) 虽然它应该是一个 UILabel 初始化。

有什么线索吗?

4

2 回答 2

2

使用 XIB 创建自定义 CollectionViewCell。请执行下列操作

1) 创建从 UIcollectionViewCell 继承的 C# 类

[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)添加一个IphoneView XIB文件,其名称与步骤1中创建的Class名称相同

3) 在 XCODE 中打开 XIB 并进行以下更改

在此处输入图像描述

3.1) 选择 FileOwner 设置与步骤 1 相同的类名称 在此处输入图像描述 3.2) 选择视图 设置类名称 UIView

4) 相应地设计您的 XIB

于 2016-08-11T09:54:14.380 回答
0

我无法完全理解您所看到的问题。什么是“自定义 XIB 插座”?为什么这个问题被标记为“自定义控件”?您可以展示一些示例代码或图片来帮助解释问题吗?


我用于 UICollectionViewCell 的方法与我用于 UITableViewCell 的方法相同 - 请参阅教程 - http://slodge.blogspot.co.uk/2013/01/uitableviewcell-using-xib-editor.html


更新:根据您作为评论发布的代码(不确定它是否完整),我认为这对您完成该教程很有用。有几个步骤需要完成,包括注册自定义类名和使用RegisterNibForCellReuse- 其中一个可能会为您解决这个问题。

于 2013-03-04T14:47:27.183 回答