我有一个带有EnableSearch = true的DialogViewController。搜索工作正常,并在对话框中显示结果。如果我导航到结果元素或另一个选项卡然后回到搜索选项卡并尝试点击任何搜索结果元素,我在DialogViewController的第463行得到“Argument超出范围。\ nParameter name:index” .cs(var element = section.Elements [indexPath.Row];)。
public class SearchView : DialogViewController
{
IFlickService _flickService;
static IEnumerable<Flick> _flicks;
public SearchView(IFlickService flickService) : base (UITableViewStyle.Grouped, null, true)
{
_flickService = flickService;
_flicks = new List<Flick>();
CreateUI();
}
public override void SearchButtonClicked (string text)
{
base.SearchButtonClicked (text);
_flicks = _flickService.Search(text);
CreateUI();
}
void CreateUI()
{
EnableSearch = true;
Root = new RootElement("Search For Flicks") {
new Section(){
from f in _flicks
select (Element) new FlickElement(f, delegate {
NavigationController.PushViewController(new FlickDetailView(_flickService, f), true);
})
}
};
}
}
答案 0 :(得分:1)
答案是覆盖ViewWillAppear()
并将我对CreateUI()
的调用从构造函数移至ViewWillAppear()
。我还必须将EnableSearch = true
移回构造函数中,因为它应该在ViewWillAppear()
之前设置。