0

I want to create a custom table cell for my iPhone application by Monodevelop and xcode 4 based on monotouch. I found this articles:

http://www.alexyork.net/blog/2011/07/18/creating-custom-uitableviewcells-with-monotouch-the-correct-way/

http://slodge.blogspot.co.uk/2013/01/uitableviewcell-using-xib-editor.html

by this two article I create a class like this:

[Register("ListingCell")]
public partial  class ListingCell : UITableViewCell
{
    public ListingCell () : base()
    {
    }

    public ListingCell (IntPtr handle) : base(handle)
    {

    }

    public void BindDataToCell(DrivedProperty  _property)
    {
        LocatoinLbl.Text  = _property .LocationString ;

    }
    public void AddImageToCell(UIImage _image){
        ListingThumbnail .Image = _image ;
    }
}

and the I designed my UITableViewCell and defined outlets. Then in my GetCell method I used codes like this:

    public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
        {
            ListingCell  cell;
cell = (ListingCell) tableView.DequeueReusableCell (identifier )  ;
            if (cell == null) {
                cell = new ListingCell ();
                var views = NSBundle.MainBundle.LoadNib("ListingCell", cell, null);
                cell = Runtime.GetNSObject( views.ValueAt(0) ) as ListingCell;
            }
var item = Controller.Items [indexPath.Row];
            cell.Tag = indexPath.Row;
            cell .BindDataToCell (item );
cell.AddImageToCell ( item .Image);
return cell;
        }

Now, Application builds well, but when I run it debuger gets error with this line:

    var views = NSBundle.MainBundle.LoadNib("ListingCell", cell, null);

and say:

MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInternalInconsistencyException Reason: Could not load NIB in bundle: 'NSBundle </Users/john/Library/Application Support/iPhone Simulator/6.0/Applications/08FFAA89-4AC4-490D-9C1A-4DE4CBC6EBB7/Realtyna_iPhone_Project.app> (loaded)' with name 'ListingCell'

Realy I don't know for what is this. When I delete it. The application make arror with cell object in my ListingCell class. I have run many searchs in the internet but didn'nt find any solutions. Do any body have any idea about this?

I found that it is possible to create the custom cell programmatically. and it will work but the problem is that creating difficult cells programmatically in run time is to difficult. It is simple that we create this cells by excode but idon't know why it not works :-S

Husein Behboudi Rad
  • 5,434
  • 11
  • 57
  • 115

1 Answers1

0

I'm not quite sure what the problem you are seeing is.

However, If you are only on iOS6, then it might help if you switch to the new API for registering NIBs and dequeuing table cells instead of using the LoadNib code.

I have found switching has helped make my code work much more reliably.

So, register your cell:

tableView.RegisterNibForCellReuse(UINib.FromName("ListingCell", NSBundle.MainBundle), ListingCell.Identifier);

Then, change dequeue code to:

public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
    ListingCell  cell = (ListingCell) tableView.DequeueReusableCell (ListingCell.Identifier, indexPath)  ;
    var item = Controller.Items [indexPath.Row];
    cell.Tag = indexPath.Row;
    cell .BindDataToCell (item );
    cell.AddImageToCell ( item .Image);
    return cell;
}

This is based on one of the articles you mentioned - http://slodge.blogspot.co.uk/2013/01/uitableviewcell-using-xib-editor.html (disclaimer - I wrote it!)


Realy I don't know for what is this

Nor do I - so not sure if this will help or not...


Aside> Your AddImageToCell code smells wrong to me - smells like you might hit a memory issue if your list is large.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Stuart
  • 66,722
  • 7
  • 114
  • 165
  • Thanks for your reply. It is not working for me. when i change ' cell = (ListingCell) tableView.DequeueReusableCell (identifier ) ' to ' cell = (ListingCell) tableView.DequeueReusableCell (identifier , indexPath) ' application cause ivalid argument error. I ignore this and changed ' var views = NSBundle.MainBundle.LoadNib("ListingCell", cell, null) ' to ' var views = NSBundle.MainBundle.LoadNib("ListingCell", cell, null) ' and comments the next line. Now, the application get error with my listing objects like 'LocatoinLbl' in run time – Husein Behboudi Rad Feb 22 '13 at 09:38
  • Regarding to adding images and memory error. I had care it in my get cell method. Based on xamarin lazy table example. – Husein Behboudi Rad Feb 22 '13 at 09:39
  • Maybe it is because of the 'nibName' parameter that i use in the 'LoadNib' method. Just i use my custom cell class. but in seems that in this question http://stackoverflow.com/questions/7184206/loading-custom-table-view-cells-from-nib-files?rq=1 answer is saying this is a different thing. Am I wrong about the 'nibName' ? – Husein Behboudi Rad Feb 22 '13 at 09:51
  • Sorry - I'm not sure I can follow your thoughts through these comments. Too hard to follow - sorry :/ – Stuart Feb 22 '13 at 09:53