0

我有一个表格视图和一个搜索栏。似乎我已经正确编写了代码,但是当我在搜索栏中输入内容时,没有结果(即使应该有)。

@interface PlaylistViewController : UITableViewController 
<UITableViewDelegate,UITableViewDataSource, UISearchBarDelegate>


@property (strong, nonatomic) Playlist* playlistTab;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) NSMutableArray *displayItems;

@end


@implementation PlaylistViewController 
@synthesize searchBar = _searchBar;
@synthesize tableView = _tableView;
@synthesize playlistTab = _playlistTab;
@synthesize displayItems = _displayItems;

- (void)viewDidLoad
{
    [super viewDidLoad];

    AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [self setPlaylistTab:appDel.playlist];
    _displayItems = _playlistTab.collection;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_displayItems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"songCell"];
    Song* song = [_displayItems objectAtIndex:indexPath.row];
    cell.textLabel.text = song.title;
    cell.detailTextLabel.text = song.artist;

    return cell;
}

-(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if ([searchText length]==0) {
        [_displayItems removeAllObjects];
        [_displayItems addObjectsFromArray:_playlistTab.collection];
    } else {
        [_displayItems removeAllObjects];
        for (Song *song in _playlistTab.collection) {
            NSRange rangeTitle = [song.title rangeOfString:searchText     options:NSCaseInsensitiveSearch];
            // NSRange rangeArtist = [song.artist rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if (rangeTitle.location != NSNotFound) {
                [_displayItems addObject:song];
            }
        }
    }

    [self.tableView reloadData];
}

我应该怎么做才能使它正常工作?

4

2 回答 2

1

虽然这几乎相同,但也试试这个

 for(int i=0;i<[_playlistTab.collection count];i++){
      NSLog(@"entered here 1");
      Song *song = (Song *)[_playlistTab.collection objectAtIndex:i];
      NSRange rangeTitle = [song.title rangeOfString:searchText     options:NSCaseInsensitiveSearch];
      NSLog(@"%@",rangeTitle);
      if(rangeTitle.length != 0) {
          NSLog(@"entered here 2");
          [_displayItems addObject:song];
      }
}
于 2012-03-18T15:34:55.450 回答
1

它看起来有点像它会起作用,但这种方法与苹果建议的方法有很大不同。我建议你修改一些东西:

1) 创建搜索结果模型。它就像您的 _displayItems,但包含与搜索匹配的子集。

@property (strong, nonatomic) NSMutableArray *searchResultDisplayItems;

2) 实现 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString。在那里进行搜索:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [searchResultDisplayItems removeAllObjects];
    // now we don't have to throw away the model all the time
    for (Song *song in _playlistTab.collection) {
        // and so on, your search code as you wrote it,
        // but when you find a match...
        [self.self.searchResultDisplayItems addObject:song];
    }
    return YES;
    // no need to explicitly reload data now.
    // answer YES and the search vc will do it for you
}

3)当表要求计数时,根据要求的表决定使用哪个模型

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // it's less typing to ask if tableView == self.tableView, but for clarity,
    // I'll ask the converse question about which table we're using

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [self.self.searchResultDisplayItems count];
    } else {
        return [self.displayItems count];
    }
}

4)当表格要求一个单元格时,根据哪个表格要求决定使用哪个模型:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"songCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSMutableArray * myModel = (tableView == self.searchDisplayController.searchResultsTableView)? self.searchResultDisplayItems : self.displayItems;
    Song* song = [myModel objectAtIndex:indexPath.row];
    cell.textLabel.text = song.title;
    cell.detailTextLabel.text = song.artist;

    return cell;
}
于 2012-03-18T16:18:13.147 回答