如何将基于故事板的标题和CustomTableCell添加到“search栏和search显示控制器”

介绍

Mine是一个简单的项目:它由一个NavigationController,ViewController和一个“Search Bar and Search Display Controller”

我的.h文件是

 #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate> @end 

和我的.m文件是

 #import "ViewController.h" @interface ViewController () @property(nonatomic,strong)NSMutableArray *data; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.searchDisplayController.displaysSearchBarInNavigationBar = YES; self.data=[[NSMutableArray alloc]init]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [_data count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIdentifier"; // Dequeue or create a cell of the appropriate type. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; cell.accessoryType = UITableViewCellAccessoryNone; } // Configure the cell. cell.textLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row]; cell.textLabel.text = [NSString stringWithFormat:@"Row %d: %@", indexPath.row, [_data objectAtIndex:indexPath.row]]; return cell; } #pragma mark - delegate - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(mockSearch:) userInfo:searchString repeats:NO]; return NO; } - (void)mockSearch:(NSTimer*)timer { [_data removeAllObjects]; int count = 1 + random() % 20; for (int i = 0; i < count; i++) { [_data addObject:timer.userInfo]; } [self.searchDisplayController.searchResultsTableView reloadData]; } @end 

这是整个计划。 它有什么作用? 用户进行search,数据显示在TableView中(类似于谷歌search)。

问题

  • 我需要为我的表使用CustomTableViewCell。 我需要从故事板中构buildTableViewCell(易于可视化)。 我坚持故事板的一部分。 如何在没有TableView的情节提要上放置一个TableViewCell放置它? 我有一个想法,我尝试了,但没有奏效。 这是我做的。 我在故事板中放置了一个“永不使用”的TableViewController,其唯一目的是保存我的CustomTableViewCell。 然后在代码我子类TableViewCell和使用IBOutlet链接故事板TableViewCell的子视图到我的CustomTableViewCell。 然后我用我的细胞作为CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 。 这没有奏效。 tableView保持空白。 而我对失败的猜测是CustomTableViewCell不属于出列的tableView

  • 我需要UISearchBar始终停留在NavigationBar内。 但到目前为止, self.searchDisplayController.displaysSearchBarInNavigationBar = YES; 没有这样做。 当我第一次启动应用程序时,searchBar在NavigationBar中。 但是,一旦我点击它,它就会在我的场景/屏幕中间自拍,永远不会离开。

  • 我需要添加一个标题到我的TableView。 我认为这是与CustomTableViewCell相同的方式,但有一个UIView父类。 但是,CustomTableViewCell部分再次失败。

恳求

感谢您提供给我的任何帮助。

UPDATE

我所要做的就是允许我的用户启动服务器端search并在tableView中查看结果。 这是这样一件基本的事情,我在这里的很多人形象已经做了好几次了。 作为iOS新手,我被卡住了。 但是在这一点上,我已经发布了我的整个项目(任何人都可以重新构build它),而且我已经详细解释了我尝试解决问题的所有方法。 所以如果有人有一个样本项目,他们不介意分享,这将非常有帮助。 或者如果你知道一个git项目,请把它链接到它。 谢谢。

如果你想在IB中创build一个独立的视图(而不是视图控制器),那么你应该在一个xib文件中,而不是一个故事板。 您可以根据需要在应用中添加尽可能多的storyboard和xib文件; 他们可以自由混合。 要在xib文件中创build一个新的单元格,只需进入新build文件 – >用户界面 – >清空,然后拖入一个UITableViewCell。 添加你想要的任何子视图,并在你的表视图控制器(或任何类是你的表视图数据源),注册xib文件,registerNib:forIdentifier :(通常,你在viewDidLoad这样做)。