滚动表视图然后突然单击搜索栏会导致应用程序崩溃?
Posted
技术标签:
【中文标题】滚动表视图然后突然单击搜索栏会导致应用程序崩溃?【英文标题】:Scrolling Table view and then suddenly click on search bar causes app to crash? 【发布时间】:2012-06-01 10:50:41 【问题描述】:我已经在表格视图中实现了搜索栏和搜索文本。我遇到的问题是,当我滚动表格视图然后突然单击搜索栏会导致应用退出。
知道如何在点击搜索栏时停止滚动。
我的代码如下:
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
NSLog(@"becomes first responder");
[table setScrollEnabled:NO];
return YES;
// return NO to not become first responder
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
isSearchOn=YES;
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
searchBar.text=@"";
isSearchOn=NO;
[searchBar resignFirstResponder];
[table setScrollEnabled:YES];
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
if ([searchText length]>0)
isSearchOn=YES;
else
isSearchOn=NO;
NSLog(@"search text %@",searchText);
[self searchText:searchText];
[table reloadData];
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
NSLog(@"end editing called.....");
searchBar.text=@"";
[table setScrollEnabled:YES];
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
[searchBar resignFirstResponder];
-(void)searchText:(NSString*)str
NSLog(@"search text");
[resultArray removeAllObjects];
if (segmentControl.selectedSegmentIndex==0)
for (NSDictionary *CellDix in FriendSuggestion)
NSRange titleResultRange = [[CellDix valueForKey:@"itemid.item"] rangeOfString:str options: (NSCaseInsensitiveSearch | NSLiteralSearch)];
NSRange titleRangePopular=[[CellDix valueForKey:@"usagecategoryid.usagecategory"] rangeOfString:str options:(NSCaseInsensitiveSearch | NSLiteralSearch)];
if (titleResultRange.length > 0 || titleRangePopular.length>0)
[resultArray addObject:CellDix];
else
for (NSDictionary *CellDix in popularSuggestion)
NSRange titleResultRange = [[CellDix valueForKey:@"itemid.item"] rangeOfString:str options: (NSCaseInsensitiveSearch | NSLiteralSearch)];
NSRange titleRangePopular=[[CellDix valueForKey:@"usagecategoryid.usagecategory"] rangeOfString:str options:(NSCaseInsensitiveSearch | NSLiteralSearch)];
if (titleResultRange.length > 0 || titleRangePopular.length>0)
[resultArray addObject:CellDix];
【问题讨论】:
【参考方案1】:单击搜索栏将在 isSearch bool 变量上,因为 U 将在 cellforRowAtIndexpath 中应用条件将调用并检查 isSearch 然后它相应地工作并且结果数组将在那里为空,因为你没有在搜索栏上写任何东西即要检查并使用 values 加载和 resultArray 。这就是它退出应用程序的原因,因为它在 cellforRowAtIndexpath 中创建了空数组来填充单元格值。所以更好的是你必须在搜索栏中的 textDidChange 上而不是在 searchBarTextDidBeginEditing 委托方法中使用 isSearch。所以评论这个方法。
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
isSearchOn=YES;
【讨论】:
以上是关于滚动表视图然后突然单击搜索栏会导致应用程序崩溃?的主要内容,如果未能解决你的问题,请参考以下文章