【问题标题】:Custom UITableViewCell, GetCell throws UnknownNSKeyException自定义 UITableViewCell,GetCell 抛出 UnknownNSKeyException
【发布时间】:2013-05-02 23:21:31
【问题描述】:

我在我的项目中创建了一个自定义UITableViewController、Cell 和 Source。 Controller 和 Cell 最初是作为 iPhone UIViewController 模板创建的,因此我将部分类与 XIB 文件配对。我有相同的设置已经在使用另一个表,并且我遵循相同的过程(我认为)来创建这个,这就是为什么我不知道为什么会收到这个错误。

这是我在TableViewSource 类中的GetCell

public override UITableViewCell GetCell (UItableView tv, NSIndexPath path)
{
    MyCell oCell;
    NSArray oViews;
    //
    oCell = tv.DequeueReusableCell ("MyCell") as MyCell;
    //
    if (oCell == null)
    {
        oViews = NSBundle.MainBundle.LoadNib ("MyCell", tv, null);
        oCell = Runtime.GetNSObject (oViews.ValueAt (0)) as MyCell;
    }
    //
    oCell.UpdateWithData (MyDataCollection[path.Row]);
    //
    return oCell;
}

我在尝试加载笔尖的那一行遇到异常:

Objective-C 抛出异常。名称:NSUnknownKeyException 原因: [ setValue:forUnidentifiedKey:]:这个类是 不符合键 lbl_address 的键值编码。

我认为这是指 IB 中的一个问题,我在其中创建了单元格接口,因为 lbl_address 是 UITableViewCell 内的 UILabel。我正在尝试在 IB 中玩一些东西,但如果你第一次没有正确地做到这一点,那可能会非常令人气愤。希望有人能认识到这个错误并纠正我。

编辑

在没有找到答案的情况下,我选择避免从笔尖加载,并对我的代码进行了以下更改:

MyTableViewSource 中的新 Get Cell 方法

public override UITableViewCell GetCell (UITableView tv, NSIndexPath path)
{
    MyCell oCell;
    //
    oCell = tv.DequeueReusableCell ("MyCell") as MyCell;
    //
    if (oCell == null)
        oCell = new MyCell ("MyCell");
    //
    oCell.UpdateWithData (MyDataCollection[path.Row]);
    //
    return oCell;
}

MyCell 的新构造函数

public MyCell (string sIdentifier)
    : base (UITableViewCellStyle.Default, sIdentifier)
{
    // Set up the contents of the cell in the absence of loading from the nib
    txt_name = new UILabel ()
    {
        Font = UIFont.SystemFontOfSize (15),
        TextColor = UIColor.LightGray
    };
    img_profile = new UIImageView ();
    //
    // Make sure to add the controls to the content view!
    ContentView.Add (txt_name);
    ContentView.Add (img_profile);
}

希望这可以帮助那里的人:)

【问题讨论】:

    标签: ios uitableview xamarin.ios xib iboutlet


    【解决方案1】:

    这通常是由于一个元素已被删除,但 xib 中的先前连接出口仍然有效。

    我会尝试以下步骤。

    打开xib。右键单击单元格。如果您在该键名附近看到黄色警告,请将其删除(x 按钮)。

    如果这能解决问题,请告诉我。

    【讨论】:

    • 很抱歉,迟迟没有回复!按照你所说的,我在 xib 或那里的任何其他网点中找不到任何警告。我发现在这种情况下我必须避免从笔尖加载,只需为使用基本UITableViewCell 构造函数(UITableViewCellType、string)的单元格创建一个构造函数。
    猜你喜欢
    • 2014-07-28
    • 2016-06-11
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 2011-10-06
    • 1970-01-01
    相关资源
    最近更新 更多