简体   繁体   English

Xamarin 自定义 TableViewCell 中的出口为空

[英]Outlets in Xamarin custom TableViewCell are null

I am trying to use a custom UITableViewCell.我正在尝试使用自定义 UITableViewCell。 I am able to get the cell to instantiate, so that it is not null.我能够让单元格实例化,因此它不为空。 However, all of the UILabel outlets in the cell are null.但是,单元格中的所有 UILabel 插座都为空。 Thus, I am getting a null pointer exception.因此,我收到了空指针异常。

This is similar to iOS custom TableViewCell class subviews return null and Label in custom cell is null when populating UITableViewController .这类似于iOS 自定义 TableViewCell 类子视图在填充 UITableViewController 时返回 null并且自定义单元格中的 Label 为 null Nevertheless, neither solutions have solved my problem.尽管如此,这两种解决方案都没有解决我的问题。 They suggest to make sure the identifier matches, which it does.他们建议确保标识符匹配,它确实如此。 They also tell me to register the cells for reuse, which I also do.他们还告诉我注册细胞以供重用,我也这样做了。

I have made sure that my identifier in the Xib file of the cell matches what I am using.我已确保单元格 Xib 文件中的标识符与我使用的标识符相匹配。

Xcode 中的 Table View Cell 显示标识符

In my Source class:在我的 Source 类中:

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
    tableView.RegisterClassForCellReuse(typeof(BoardListCell), new NSString(BoardListCell.Key));
    var cell = (BoardListCell) tableView.DequeueReusableCell(BoardListCell.Key);
    if (cell == null) 
    {
        var nib = UINib.FromName(BoardListCell.Key, NSBundle.MainBundle);
        cell = (BoardListCell)nib.Instantiate (null, null) [0];
    }

    cell.setData("top", "not top"); //Sample data
    return cell;
}

In my BoardListCell class:在我的 BoardListCell 类中:

public partial class BoardListCell : UITableViewCell
{
    public static readonly UINib Nib = UINib.FromName ("BoardListCell", NSBundle.MainBundle);
    public static readonly NSString Key = new NSString ("BoardListCell");

    public BoardListCell() : base()
    {

    }

    public BoardListCell (IntPtr handle) : base (handle)
    {

    }

    public void setData(String top, String bottom)
    {
        exp.Text = top;
        playTime.Text = bottom;
    }
}

When I debug, the cell is being created.当我调试时,正在创建单元格。 However, its outlets are null.然而,它的网点是空的。 If I instantiate them in the constructor, I don't get the error, but this obviously defeats the purpose of making prototypes in xcode as it wipes out the layout, etc.如果我在构造函数中实例化它们,我不会收到错误消息,但这显然违背了在 xcode 中制作原型的目的,因为它会清除布局等。

I was having the same issue described above.我遇到了上述相同的问题。 The solution for me was to remove the call to TableView.RegisterClassForCellReuse(typeof(YourCustomeTableCell), "YourTableCellName");我的解决方案是删除对TableView.RegisterClassForCellReuse(typeof(YourCustomeTableCell), "YourTableCellName");的调用TableView.RegisterClassForCellReuse(typeof(YourCustomeTableCell), "YourTableCellName");

If you created the Cell in the Xcode designer, there is no need to register the Cell.如果您在 Xcode 设计器中创建了 Cell,则无需注册 Cell。

Hope this helps anyone still having this issue.希望这可以帮助任何仍然遇到此问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM