2012-03-18 30 views
0

私はテーブルビューとその中に検索バーを持っています。私はコードを正しく書いているようですが、検索バーに何かを入力すると、結果はありません。UITableView検索バーの問題

@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]; 
} 

これを正しく実行するにはどうすればよいですか?

答えて

1

これはほぼ同じですが、それがうまくいくようにそれは一種のように見えますが、アプローチは何apple suggestsと大きく異なる

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]; 
     } 
} 
+0

うまくいきませんでした。ここで私の推測 - 私は接続で何かを台無しにしているということです。 – nemesis

+0

あなたはNSLogを使用して印刷されたコメントを見ましたか? – rakeshNS

+0

いいえ、私はそれらを見ませんでした。 – nemesis

1

あまりにもこれを試してみてください。いくつか修正することをお勧めします:

1)検索結果モデルを作成します。 _displayItemsと似ていますが、検索結果と一致するサブセットが含まれています。

@property (strong, nonatomic) NSMutableArray *searchResultDisplayItems; 

2)実装 - (BOOL)searchDisplayController:(UISearchDisplayController *)コントローラ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; 
} 
+0

私はあなたが言ったようにすべてをやったが、今はテーブルビューが空です。 – nemesis

+0

@nemesis、別の問題があります。デキューされない場合は、セルを割り当てる必要があります。編集します。 – danh

+0

期待した結果が得られない場合。いくつかのNSLogを追加して、あなたに見えるものを教えてください。あなたのモデルがいくつかの曲から始まっていることを確認してください、2)あなたの検索コードが何かを見つけていることを確認してください。しかし、私はセルの割り当てが明らかに問題であったと思います。 – danh