Xamarin自定义TableViewCell中的出口为空

Use*_*920 5 uitableview xamarin.ios ios xamarin

我正在尝试使用自定义UITableViewCell.我能够让单元格实例化,因此它不是null.但是,单元格中的所有UILabel出口都是空的.因此,我得到一个空指针异常.

这类似于iOS自定义TableViewCell类子视图返回null自定义单元格中的Label在填充UITableViewController时为null.然而,这两种解决方案都没有解决我的问 他们建议确保标识符与之匹配.他们还告诉我注册单元格以便重复使用,我也这样做.

我确保单元格的Xib文件中的标识符与我正在使用的相匹配.

Xcode中的表视图单元格显示标识符

在我的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;
}
Run Code Online (Sandbox Code Playgroud)

在我的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我调试时,正在创建单元格.但是,它的出口是空的.如果我在构造函数中实例化它们,我没有得到错误,但这显然违背了在xcode中制作原型的目的,因为它消除了布局等.

jja*_*394 0

我遇到了上面描述的同样的问题。对我来说,解决方案是删除对TableView.RegisterClassForCellReuse(typeof(YourCustomeTableCell), "YourTableCellName");

如果您在 Xcode 设计器中创建了 Cell,则无需注册 Cell。

希望这对仍然遇到此问题的人有所帮助。