Monotouch custom UITableViewCell GetCell does not initialize new custom view cells

I used several tutorials to build a custom table view cell using a storyboard to represent a prototype table.

I am new to monotouch and managed to get a working solution for standard cell types. Performing problems with user view cells as I cannot properly initialize a new cell. Some older tutorials seem to download the nib file for cells, but I use a storyboard with the code below.

Where am I going wrong?

(I would use the monotouch dialog, but couldn't figure out how to add beautiful uipickerviews to an accessory, etc. in a simple way).

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

}
+2
source share
4 answers

This can be done using only storyboards with prototype cells, see this sample from the Xamarin forums .

Here is the corresponding code (loan to Stefan 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;
        }
    }
}

User 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 set your own cell class and the identifier for the name of your cell class in the storyboard.

+1
source

.xib "SingleTimeViewCell". , , , , .

0

. xib .

0

, Xib, jsute ...

iOS6 :

  • UITABLEVIEWCELL Xamarin. (, MyCustomCell)
  • InterfaceBuilder MyCustomCell.Key( MyCsutomCell.cs, Xamarin Studio)
  • In your ViewController, register nib as follows:

tableView.RegisterNibForCellReuse (MyCsutomCell.Nib, MyCsutomCell.Key);

  1. then in the GetCell méthode in your symple data file, delete the cell (it will create one if it does not exist):

MyCsutomCell cell = tableView.DequeueReusableCell (MyCsutomCell.Key) as MyCsutomCell;

0
source

Source: https://habr.com/ru/post/1628666/


All Articles