0

編輯:我認爲問題出在上次引用的代碼中,因爲當我嘗試加載表數據而不是搜索結果時(通過交換數組),我仍然沒有搜索結果,但我仍然不知道爲什麼...原始文章全文:uitableview searchbar ios5 ARC/storyboard

我試圖找到一個搜索欄與我的表視圖工作。我可以在搜索欄中輸入文字,但始終找不到結果。這裏是我的.h

@interface SecondViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate> { 
IBOutlet UISearchBar *searchBar; 
NSMutableArray *dummyArray; 
NSMutableArray *searchArray; 
} 

@property (nonatomic, strong) NSMutableArray *dummyArray; 
@property (nonatomic, strong) NSMutableArray *searchArray; 
@property (nonatomic, strong) NSMutableArray *detailArray; 
@property (nonatomic, strong) NSMutableArray *imageArray; 


- (void) setupArray; 

- (void) setupSearchArray; 

- (void) setupDetailArray; 

@end 

在我的.m,dummyArray是填充主cell.textLabel.text,我想搜索的數組的數組。

我設置searchArray這裏:

- (void) setupSearchArray{ 
searchArray = [NSMutableArray arrayWithCapacity:[dummyArray count]]; 
} 

然後嘗試讓searchArray dummyArray的過濾版本在這裏:

- (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 
[searchArray removeAllObjects] ; 

for (NSString *savedSeachTerm in dummyArray) { 



    NSRange result = [savedSeachTerm rangeOfString:searchString options: 
         NSCaseInsensitiveSearch]; 
    if (result.location != NSNotFound) 
     [searchArray addObject:savedSeachTerm]; 

} 

return YES; 
} 

,並嘗試填充搜索結果的表格中輸入搜索文本時這裏:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 



} 

if (tableView == self.searchDisplayController.searchResultsTableView) { 
    cell.textLabel.text = [searchArray objectAtIndex:indexPath.row]; 
    cell.detailTextLabel.text = [detailArray objectAtIndex:indexPath.row]; 
} 
else { 
    cell.textLabel.text = [dummyArray objectAtIndex:indexPath.row]; 
    cell.detailTextLabel.text = [detailArray objectAtIndex:indexPath.row]; 
} 
     if (indexPath.row == 0) { 
    cell.imageView.image = [UIImage imageNamed:@"row0.png"]; 
    } 
    return cell;  
} 

我做了什麼錯了?感謝您的期待!

回答

0

我忘了

[self setupSearchArray]; 

對不起!