1

I understand there are several similar questions already asked and I went through all of them, but none of them solved my problem.

I have a simple UIViewController which contains a UISearchBar at top and a UITableView. This is the basic code:

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();            
        searchBar.TextChanged += delegate {
            tableView.DataSource = new CodeSearchTableViewDataSource(searchBar.Text);
            tableView.ReloadData();
        };
        tableView.DataSource = new CodeSearchTableViewDataSource(searchBar.Text);
    }

public class CodeSearchTableViewDataSource : UITableViewDataSource
{
    static NSString CELL_ID = new NSString("MYID");

    public List<CodeItem> CodesFound { get; set; }

    public CodeSearchTableViewDataSource()
    {
    }
    public CodeSearchTableViewDataSource(string searchText)
    {
        CodesFound = CodeSearch.Instance.Find(searchText);          
    }
    public override int RowsInSection (UITableView tableView, int section)
    {
        return CodesFound.Count;
    }
    public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
    {
        UITableViewCell cell = tableView.DequeueReusableCell(CELL_ID);
        if (cell == null)
            cell = new UITableViewCell(UITableViewCellStyle.Default, CELL_ID);
        cell.TextLabel.Text = CodesFound[indexPath.Row].Id + " " + CodesFound[indexPath.Row].Desc;
        return cell;            
    }
}

I don't have custom delegate. What I don't understand is that when this view loads up the first time, the table is populated with a valid list. However, after I enter a character in search box, a new list is sent to the table, and it crashes.

poupou
  • 43,413
  • 6
  • 77
  • 174
newman
  • 6,841
  • 21
  • 79
  • 126

1 Answers1

1

There's no (managed) reference to the cell you are creating in GetCell once it returns from the method. As such the Garbage Collector (GC) can collect it whenever it wants to.

For more details see my answer (and its links) for the question on: Monotouch - UITableview Cell null reference exception

Community
  • 1
  • 1
poupou
  • 43,413
  • 6
  • 77
  • 174
  • Poupou: Thank you very much for your response, but I just tried to cache the cells, and it didn't solve the problem - I still get the same exception. – newman Nov 27 '11 at 16:11
  • Not sure (hard to be without a full test case) but I think this is related to fact you're replacing `tableView.DataSource` in the `TextChanged` event. The previous one, not being referenced anymore, can be collected before the `UITableView` has completed it's work with it. To **confirm** this keep a reference to the old `CodeSearchTableViewDataSource` in a field and see if your error goes away. – poupou Nov 27 '11 at 16:28
  • Okay, I managed to solve my problem by simply recreating the Controller. I believe this is a MonoTouch bug, but I don't know how to reproduce it. – newman Nov 27 '11 at 17:02
  • poupou: To follow up with your comment...From my testing, GC of cells is definitely not a problem here. Once I got it work, it works with or without caching the cells. – newman Nov 27 '11 at 17:09
  • It will depend on your code. So it will likely be fine (as such) until something needs to callback into the managed code (e.g. if you add a button and `Clicked` event in your cell). Just keep this in mind :) – poupou Nov 27 '11 at 17:12