【问题标题】:Problem: scrolling a tableview and then select the searchbar (iphone)问题:滚动表格视图,然后选择搜索栏(iphone)
【发布时间】:2011-02-25 03:25:23
【问题描述】:
我已经被这个问题困扰了好几个小时,但我看不到光明。请帮我解决这个问题:
我有一个表格视图和一个搜索栏。搜索栏位于导航栏中。当我快速滚动 tableview 时,如果在 tableview 仍在减速时选择搜索栏,则会引发异常:
由于未捕获而终止应用
异常'NSRangeException',原因:
' -[NSMutableArray objectAtIndex:]:
索引 31 超出范围为空
数组'
如何以编程方式停止 tableview 的减速?
感谢您的宝贵时间!
【问题讨论】:
标签:
ios
uitableview
uisearchbar
【解决方案1】:
在没有代码 sn-p 的情况下解决错误有点困难。然后我从错误中认为,在滚动 tableview 并选择搜索栏之后,您将 Array 设为 null 或删除所有对象。
查看错误似乎是在选择 SearchBar 时
由于未捕获的异常“NSRangeException”而终止应用程序,原因:“-[NSMutableArray objectAtIndex:]: index 31 beyond bounds for empty array”
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;
而另一方面,表格一旦滚动创建新单元格以查找 Array 中的 indexPath 对象。但由于所有对象都已从数组中删除,它只会使您的应用程序崩溃并显示此错误。
所以我建议你使用两个不同的数组,一个是完整记录的数组,另一个是搜索结果,然后使用数组重新加载表。
您肯定会摆脱此错误。否则请尝试确保在搜索完成之前不会从数组中删除所有对象。
这是您可以用于搜索的逻辑。 :)
-
(void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {
//先删除所有对象。
copyListOfItems = [[NSMutableArray alloc] init];
[copyListOfItems removeAllObjects];
if([搜索文本长度] > 0) {
searching = YES;
tblView.scrollEnabled = YES;
NSString *searchText = searchBar.text;
NSLog(@"%@",searchText);
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSMutableDictionary *dictionary in YourMainArray)
{
NSRange titleResultsRange = [[dictionary valueForKey:@"English"] rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[copyListOfItems addObject:dictionary];
}
[searchArray release];
searchArray = nil;
}
否则 {
searching = NO;
tblView.scrollEnabled = YES;
[searchBar resignFirstResponder];
}
[tblView 重载数据];
}