1
我知道有几个类似的问题已经问过,我经历了所有这些问题,但没有一个解决了我的问题。MonoTouch:NSInvalidArgumentException原因: - [... tableView:numberOfRowsInSection:]:无法识别的选择发送到实例0x9940d70
我有一个简单的UIViewController
其中包含顶部的UISearchBar
和UITableView
。这是基本代码:
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;
}
}
我没有自定义委托。我不明白的是,当这个视图第一次加载时,表中填充了一个有效的列表。但是,在搜索框中输入一个字符后,将向该表发送一个新列表,并将其崩溃。
Poupou:非常感谢您的回复,但我只是想缓存的细胞,它并没有解决问题 - 我仍然得到相同的例外。 – newman
不确定(很难没有一个完整的测试用例),但我认为这与你在'TextChanged'事件中替换'tableView.DataSource'的事实有关。前一个,不再被引用,可以在UITableView完成它的工作之前收集。要**确认**,这将保留对字段中旧的“CodeSearchTableViewDataSource”的引用,并查看错误是否消失。 – poupou
好的,我设法通过简单地重新创建控制器来解决我的问题。我相信这是一个MonoTouch错误,但我不知道如何重现它。 – newman