滚动表格视图,然后突然点击搜索栏导致应用程序崩溃?

时间:2012-06-01 10:50:42

标签: iphone objective-c ios

我在表格视图中实现了搜索栏和搜索文本。我得到的问题是,当我滚动表格视图然后突然点击搜索栏导致应用程序退出。

有任何机构的想法,我们怎么能做任何事情来点击搜索栏时停止滚动。

我的代码如下:

    - (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 个答案:

答案 0 :(得分:0)

点击搜索栏将在isSearch bool变量上,因为U将在cellforRowAtIndexpath中应用条件将调用并且它检查isSearch然后它相应地工作并且结果数组将在那里为空因为U没有在搜索栏上写任何东西要检查并加载和resultArray值。这就是它退出应用程序的原因,因为它在cellforRowAtIndexpath中找到了填充单元格值的空数组。所以更好的一点就是你必须在搜索栏中的textDidChange上搜索而不是在searchBarTextDidBeginEditing委托方法中。所以评论这个方法。

  - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
    {
     isSearchOn=YES;
    }