![](/img/trans.png)
[英]Xcode 5.1 UITableView in UIViewController - Custom TableViewCell Outlets are nil
[英]Outlets in Xamarin custom TableViewCell are null
我正在尝试使用自定义 UITableViewCell。 我能够让单元格实例化,因此它不为空。 但是,单元格中的所有 UILabel 插座都为空。 因此,我收到了空指针异常。
这类似于iOS 自定义 TableViewCell 类子视图在填充 UITableViewController 时返回 null并且自定义单元格中的 Label 为 null 。 尽管如此,这两种解决方案都没有解决我的问题。 他们建议确保标识符匹配,它确实如此。 他们还告诉我注册细胞以供重用,我也这样做了。
我已确保单元格 Xib 文件中的标识符与我使用的标识符相匹配。
在我的 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;
}
在我的 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;
}
}
当我调试时,正在创建单元格。 然而,它的网点是空的。 如果我在构造函数中实例化它们,我不会收到错误消息,但这显然违背了在 xcode 中制作原型的目的,因为它会清除布局等。
我遇到了上述相同的问题。 我的解决方案是删除对TableView.RegisterClassForCellReuse(typeof(YourCustomeTableCell), "YourTableCellName");
的调用TableView.RegisterClassForCellReuse(typeof(YourCustomeTableCell), "YourTableCellName");
如果您在 Xcode 设计器中创建了 Cell,则无需注册 Cell。
希望这可以帮助任何仍然遇到此问题的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.