【发布时间】:2012-02-27 21:33:03
【问题描述】:
编辑:我认为问题出在最后引用的代码中,因为当我尝试加载表数据而不是搜索结果时(通过交换数组)我仍然没有得到搜索结果,但我仍然没有确定为什么...原帖全文:
我正在尝试让搜索栏与我的表格视图一起使用。我可以在搜索栏中输入文本,但总是找不到任何结果。这是我的 .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;
}
我做错了什么?感谢收看!
【问题讨论】:
标签: ios uitableview storyboard automatic-ref-counting uisearchbar