【问题标题】:Not able to use Custom XIB outlets with UICollectionViewCell无法将自定义 XIB 插座与 UICollectionViewCell 一起使用
【发布时间】:2013-02-17 21:05:11
【问题描述】:

从我的 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 初始化。

有什么线索吗?

【问题讨论】:

    标签: xamarin.ios custom-controls xib uicollectionviewcell


    【解决方案1】:

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

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

    3.1) 选择FileOwner 设置Class 和Step 1 一样的名字 3.2)选择视图设置类名UIView

    4) 相应地设计您的 XIB

    【讨论】:

      【解决方案2】:

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


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


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

      【讨论】:

      • 编辑您的问题并在其中粘贴完整代码 - 我无法在评论中阅读。
      • 我已经添加了 this.tableComments.RegisterClassForCell 代替。 this.tableComments.RegisterClassForCell (typeof(CustomCommentCell),commentCellId);
      • 如果我删除它,我会在 GetCell 方法中得到一个错误。一切似乎都正常,并且在智能感知中出现了出口,但只是没有初始化。
      • 尝试示例教程或发布更多代码。我无法在这样的 cmets 中调试您的问题。我可以告诉你slodge.blogspot.co.uk/2013/01/… 中的流程有效,并且我对集合单元格和表格单元格使用相同的流程。
      • 为了更好地描述,我遇到了同样的问题:stackoverflow.com/questions/14750402/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-19
      • 2015-02-10
      相关资源
      最近更新 更多