UISearchBar with large data set in table gives Index Out Of Range when clicking on search bar.

DustinLindgrenDustinLindgren USUniversity

I'm looking to use iOS's handy search bar. I found a great sample project on doing this but when I went to do something similar in my project I kept getting "Index Out of Range" errors and I'm hoping someone here has experienced a similar problem or is just more knowledgeable than me with why this is happening.

In the attached project, in FruitTableController.cs you will find at line 30 I'm making a new list of Fruits.

fruits = new List<Fruit> { new Fruit{ Name = "apple", ColorName = "red" }, new Fruit{ Name = "apple", ColorName = "green" }, new Fruit{ Name = "apple", ColorName = "yellow" }, new Fruit{ Name = "banana", ColorName = "green" }, new Fruit{ Name = "banana", ColorName = "yellow" }, new Fruit{ Name = "pear", ColorName = "yellow" }, new Fruit{ Name = "banana", ColorName = "green" }, new Fruit{ Name = "banana", ColorName = "yellow" }, new Fruit{ Name = "pear", ColorName = "yellow" }, new Fruit{ Name = "banana", ColorName = "green" }, new Fruit{ Name = "banana", ColorName = "yellow" }, new Fruit{ Name = "pear", ColorName = "yellow" }, new Fruit{ Name = "banana", ColorName = "green" }, new Fruit{ Name = "banana", ColorName = "yellow" }, new Fruit{ Name = "pear", ColorName = "yellow" }, new Fruit{ Name = "banana", ColorName = "green" }, new Fruit{ Name = "banana", ColorName = "yellow" }, new Fruit{ Name = "pear", ColorName = "yellow" }, };

for some reason, if that List is long enough, when you click on the Search Controller it will then call

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)

with an indexPath that is out of range. But if you shorten the list, it won't call GetCell at all when you click the Search Controller. I'm at a loss why this is happening and how to fix it.

I'm hoping someone here has experienced a similar problem?

Please see the attached project.

Answers

Sign In or Register to comment.