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.