我创建了一个简单的视图控制器,其中有一个表视图。然后,我创建了一个.xib文件来设计将进入表中的UITableViewCells。
无论我如何尝试,GetCell都找不到UITableViewCell笔尖。我经历了每一个名字/名字和演员的变化。我对Xamarin和c#非常陌生,所以我可能错过了一些简单的东西。
ViewController:
public partial class ScheduleViewController : BaseViewController<ScheduleViewModel>
{
[Export("initWithBundle:owner:extras:")]
public ScheduleViewController(NSBundle bundle, UIViewController owner, string extras) : base("ScheduleViewController", bundle, owner, extras)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
Dictionary<string, List<string>> itemData = new Dictionary<string, List<string>>()
{
{"phones", new List<string>() {
"Android",
"iOS",
"Windows Phone",
"Other",
"The Thing"
}},
{"computers", new List<string>() {
"osx",
"windows",
"linux"
}}
};
UITableView table = new UITableView(View.Bounds);
table.Source = new ScheduleTableViewSource(itemData);
table.SeparatorStyle = UITableViewCellSeparatorStyle.None;
Add(table);
}UITableVIewCell类:
public partial class WorkCell : UITableViewCell
{
public static readonly NSString Key = new NSString("WorkCell");
public static readonly UINib Nib;
static WorkCell()
{
Nib = UINib.FromName("WorkCell", NSBundle.MainBundle);
}
protected WorkCell(IntPtr handle) : base(handle)
{
// Note: this .ctor should not contain any initialization logic.
}
}WorkCell .xib文件


TableViewDataSource:
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
// always null
UINib nib = UINib.FromName("WorkCellContainer", NSBundle.MainBundle);
tableView.RegisterNibForCellReuse(nib, "workItemCell");
var cell = (WorkCell)tableView.DequeueReusableCell ("workItemCell");
return cell;
}发布于 2018-12-05 21:21:59
没有必要在GetCell方法中加载nib。相反,要使用来自xib的自定义单元格,只需执行以下操作:
iOS,选择Table View Cell)寄存器NIB
在UITableViewController子类中的ViewDidLoad中(例如,在设置DataSource之前)添加以下内容:
table.RegisterNibForCellReuse(WorkCell.Nib, WorkCell.Key);集重用标识符用于单元

使单元格脱队列
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell(WorkCell.Key, indexPath) as WorkCell;
//set the data in work cell here
return cell;
}在模拟器中的测试

发布于 2018-12-04 23:22:09
虽然您只显示了xib文件的一部分,但这应该有效,因此我可能会出错:
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
UINib nib = UINib.FromName("WorkCell", NSBundle.MainBundle);
tableView.RegisterNibForCellReuse(nib, "workCellContainer");
var cell = (WorkCell)tableView.DequeueReusableCell ("workCellContainer");
return cell;
}虽然这应该有效,但是没有必要在tableView中注册GetCell,您应该在ViewDidLoad中这样做--这是一个更好的解决方案。
https://stackoverflow.com/questions/53509339
复制相似问题