2

在 iOS 8 中,UISearchDisplayController 已被弃用,而 UISearchController 可以选择将搜索行为添加到应用程序。这迫使开发人员创建一个单独的 UITableviewController 来显示搜索结果——让我们为resultsTableViewController调用这个 tableViewController 。这个线程很好地介绍了如何从 UISearchDisplayController 迁移到 UISearchController:searchDisplayController deprecated in iOS 8

在我的应用程序中,我使用的是 interfacebuilder 和 storyboard。我的 UITableViewController 是在情节提要中自定义的,带有自定义单元格,给了我想要的东西。但是,我希望我的resultsTableViewController看起来与我的tableViewController完全一样。

问题:有没有办法在我的tableViewControllerresultsTableViewController的情节提要中使用相同的场景?如果没有,最好的解决方法是什么?这个问题在这里得到了部分回答:How to share one storyboard scene between multiple UIViewControllers,但我还有一段路要走。

4

1 回答 1

2

在 Apple 开发者论坛的tartempion的帮助下,我终于在一个文件中管理了一个 UTableviewController 和 searchViewController,使我能够使用相同的故事板场景来显示搜索结果。

@interface TableViewController () <UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating>
{
    UISearchController *_searchcontroller;

     NSArray * myArray;
     NSArray * myDisplayedArray; 
}
@end

@implementation TableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [...]

     _searchcontroller=[[UISearchController alloc]initWithSearchResultsController:nil];
     _searchcontroller.searchResultsUpdater =self;
     _searchcontroller.dimsBackgroundDuringPresentation=NO;

     myDisplayedArray=myArray;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [myDisplayedArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    MyClass*myInstance =[myDisplayedArray objectAtIndex:[indexPath row]];

  //and however you want to configure cells...

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

  //whatever you need...
  //and to get rid of the searchcontroller when you move to the UIView that is connected
        self.searchcontroller.active=NO;
                 //or
        [self.searchcontroller dismissViewControllerAnimated:NO completion:nil];

}

#pragma mark -

- (void)updateSearchResultsForSearchController:(UISearchController *)inSearchController
{
    if (_searchController==inSearchController)
    {
        NSString *tSearchText = inSearchController.searchBar.text;

        if ([tSearchText length]==0)
        {
            myDisplayedArray=myArray;
        }
        else
        {
            myDisplayedArray=[myArray filteredWithPattern:tSearchText];
        }

        [self.tableView reloadData];
    }
}

@end
于 2015-01-04T14:04:37.347 回答