0

全部,

我有一个带有详细信息的 UITableView,带有一个从代码创建的搜索栏。在选定的项目上工作正常,但在从搜索结果中单击项目时不起作用;没有委托方法触发。以前从未见过这种行为,所以我不知道问题出在哪里。

我对标准表格单元格和自定义表格单元格有相同的行为。

感谢您对此的任何指导并感谢。

就挂在这里 在此处输入图像描述

//ViewController.h
 @interface SongsViewController : UITableViewController <UISearchBarDelegate,UISearchDisplayDelegate>
{
   NSMutableArray *searchData;
   UISearchBar *searchBar;
   UISearchDisplayController *searchDisplayController;
   UITableViewCell *customSongCell;
 }




 //ViewController.m
 -(void)viewDidLoad
 {
    [super viewDidLoad];

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 88)];

   [searchBar setShowsScopeBar:YES];
   [searchBar setScopeButtonTitles:[[NSArray alloc] initWithObjects:@"Title",@"Style", nil]];

   searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
   searchDisplayController.delegate = self;
   searchDisplayController.searchResultsDataSource = self;
   self.tableView.tableHeaderView = searchBar; 

   //Load custom table cell
    UINib *songCellNib = [UINib nibWithNibName:@"SongItem" bundle:nil];

  //Register this nib, which contains the cell
  [[self tableView] registerNib:songCellNib forCellReuseIdentifier:@"SongItemCell"];

  // create a filtered list that will contain products for the search results table.
  SongStore *ps = [SongStore defaultStore];
  NSArray *songObjects = [ps allSongs];

  self.filteredListContent = [NSMutableArray arrayWithCapacity: [songObjects count]];
  self.masterSongArr = [[NSMutableArray alloc] initWithArray:[ps allSongs]];

 [self refreshData];

 [self.tableView reloadData];
 self.tableView.scrollEnabled = YES;

}
4

1 回答 1

4

我解决了这个问题。我忘了设置这个 searchDisplayController 代表:

[searchDisplayController setSearchResultsDelegate:self];

因此,请确保您设置了所有这些:

searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
[searchDisplayController setDelegate:self];
[searchDisplayController setSearchResultsDataSource:self];
[searchDisplayController setSearchResultsDelegate:self];
于 2012-06-28T20:52:45.790 回答