uitableview 搜索栏 ios5 ARC/故事板

发布于 2025-01-03 10:44:41 字数 2419 浏览 3 评论 0原文

编辑:我认为问题出在最后引用的代码中,因为当我尝试加载表数据而不是搜索结果(通过交换数组)时,我仍然没有得到搜索结果,但我仍然不确定为什么。 ..原始帖子全文:

我正在尝试让搜索栏与我的表格视图一起使用。我可以在搜索栏中输入文字,但总是找不到结果。这是我的 .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;    
}

我做错了什么?感谢您的关注!

edit: I think the problem is in the last referenced code, because when I try to load the table data instead of the search results (by just swapping the arrays) I still get no search results, but I'm still not sure why... Original post in full:

I'm trying to get a search bar working with my table view. I can enter text in the search bar but always find no results. Here's my .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

In my .m, dummyArray is the array that fills the main cell.textLabel.text and the array i want to search.

I set up searchArray here:

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

then attempt to make searchArray a filtered version of dummyArray here:

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

and try to populate the table with search results when search text is entered here:

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

What have I done wrong? Thanks for looking!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉溺在你眼里的海 2025-01-10 10:44:41

我忘了

[self setupSearchArray];

道歉!

I had forgotten to

[self setupSearchArray];

sorry!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文