Monotouch 自定义 UITableViewCell GetCell 不初始化新的自定义视图单元格 [英] Monotouch custom UITableViewCell GetCell not initialising new custom view cells
问题描述
我一直在学习许多教程,以使用故事板为原型表视图组合自定义表视图单元格.
I've been foloowing a number of tutorials to put together a custom table view cell using storyboard for a prototype table view.
我是 monotouch 的新手,并设法为标准细胞类型找到了一个有效的解决方案.遇到自定义视图单元格的问题,因为我无法以正确的方式初始化新单元格.一些旧教程似乎加载了一个单元格 nib 文件,但我正在使用带有以下代码的故事板.
I'm new to monotouch and managed to get a working solution for standard cell types. Running into issues with custom view cells as I'm unable to initialise a fresh cell in the correct manner. Some old tutorials appear to load a cell nib file but I'm using storyboard with the below code.
我哪里出错了?
(我会使用单点触控对话框,但我想不出一种方法来以简单的方式在配件等上添加可爱的 uipickerviews).
(I would use monotouch dialog but not couldn't figure out a way to add lovely uipickerviews on accessory, etc in a simple manner).
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
// in a Storyboard, Dequeue will ALWAYS return a cell
//*** above comment doesnt seem to hold for custom uitableview cells
UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
// now set the properties as normal
cell.TextLabel.Text = tableItems[indexPath.Row].Name;
if (tableItems[indexPath.Row].Done)
cell.Accessory = UITableViewCellAccessory.Checkmark;
else
cell.Accessory = UITableViewCellAccessory.None;
return cell;
}
// here's my implementation of GetCell but problem is that I can't seem to generate a new cell
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
_cellIdentifier = "SingleTimeViewCell";
CustomViewCell cell = tableView.DequeueReusableCell (_cellIdentifier) as
// if (cell == null)
// {
// cell = new SingleTimeViewCell();
// }
cell.myCustomProperty = "hello";
return cell;
}
// here's the auto generated CustomViewCell class from Xcode storyboard
public partial class CustomViewCell : UITableViewCell
{
public CustomViewCell () : base() // I added this ctor but it didnt seem to help matters
{
}
public CustomViewCell (IntPtr handle) : base (handle)
{
}
}
推荐答案
只能使用带有原型单元格的故事板来完成,请参阅 此示例来自 Xamarin 论坛.
It can be done using only storyboards with prototype cells, see this sample from Xamarin forums.
这是相关代码(感谢 Stephane Cordonnier):
Here is the relevant code (credit to Stephane Cordonnier):
public partial class CustomCellStoryBoardViewController : UIViewController
{
public CustomCellStoryBoardViewController(IntPtr handle) : base (handle)
{
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
this.MyTableView.DataSource = new MyDataSource();
}
private class MyDataSource : UITableViewDataSource
{
private String[] items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public MyDataSource()
{
}
public override int RowsInSection(UITableView tableView, int section)
{
return this.items.Length;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
MyCustomCell cell = (MyCustomCell)tableView.DequeueReusableCell(MyCustomCell.Key);
cell.SetTitle(this.items[indexPath.Row]);
return cell;
}
}
}
自定义单元格:
[Register ("MyCustomCell")]
public class MyCustomCell : UITableViewCell
{
[Outlet]
MonoTouch.UIKit.UILabel TitleLabel { get; set; }
public static readonly NSString Key = new NSString("MyCustomCell");
public MyCustomCell(IntPtr handle) : base(handle)
{
}
public void SetTitle(String title)
{
this.TitleLabel.Text = title;
}
}
确保您已将自定义单元类和标识符设置为故事板中单元类的名称.
Make sure you have set the custom cell class and identifier to the name of your cell class in the storyboard.
这篇关于Monotouch 自定义 UITableViewCell GetCell 不初始化新的自定义视图单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!