Monotouch定制UITableViewCell GetCell无法初始化新的自定义视图单元格

时间:2013-04-15 22:17:30

标签: uitableview xamarin.ios xamarin

我一直在使用一些教程将一个自定义表格视图单元格用于原型表视图的故事板。

我是monotouch的新手,并设法获得标准细胞类型的工作解决方案。遇到自定义视图单元格的问题,因为我无法以正确的方式初始化新单元格。一些旧的教程似乎加载单元格的nib文件,但我使用的故事板与下面的代码。

我哪里错了?

(我会使用monotouch对话框,但不能想办法以简单的方式在附件上添加可爱的uipickerviews等。)

http://docs.xamarin.com/guides/ios/user_interface/tables/part_5_-_using_xcode%2C_interface_builder%2C_and_storyboards

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)
    { 
    }

}

4 个答案:

答案 0 :(得分:1)

只能使用带有原型单元格的故事板来完成,请参阅this sample from Xamarin forums

以下是相关代码(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;
    }
}

确保已将自定义单元格类和标识符设置为情节提要中单元格类的名称。

答案 1 :(得分:0)

在.xib文件中,对于该单元格,您使用的是相同的单元格标识符“SingleTimeViewCell”。我不确定你是如何创建自定义单元格的,但我知道我听到了同样的问题。

答案 2 :(得分:0)

你不能用故事板来做。使用主故事板和单独的xib文件。

答案 3 :(得分:0)

我读到了关于tableview单元格和重用以及Xib的非常感觉, 几乎所有找到的jsute示例代码都没有正确重用...

iOS6上的

以及最简单的方法:

  1. 从Xamarin创建一个新的UITABLEVIEWCELL。 (比方说MyCustomCell)
  2. InterfaceBuilder中的
  3. 将单元格的标识符设置为MyCustomCell.Key的值(找到Xamarin Studio创建的MyCsutomCell.cs中的值)
  4. 在ViewController中以这种方式注册nib:
  5. tableView.RegisterNibForCellReuse (MyCsutomCell.Nib, MyCsutomCell.Key);

    1. 然后在您的dataSource中的GetCellméthode中,将一个单元格出列(如果不存在则创建一个单元格):
    2. MyCsutomCell cell = tableView.DequeueReusableCell (MyCsutomCell.Key) as MyCsutomCell;

相关问题