0

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

有任何想法,当点击搜索栏时,我们如何做任何事情来停止滚动。

我的代码如下:

    - (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];
  }
 }
4

1 回答 1

0

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

  - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
    {
     isSearchOn=YES;
    }
于 2012-06-01T11:05:06.300 回答