Cocoa touch 实现搜索栏?

Cocoa touch 实现搜索栏?,cocoa-touch,uisearchcontroller,Cocoa Touch,Uisearchcontroller,我有一个在故事板上创建了tableView的项目。这很简单,我是按照一个教程来做的,我的视图控制器是这样的 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefau

我有一个在故事板上创建了tableView的项目。这很简单,我是按照一个教程来做的,我的视图控制器是这样的

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    cell.textLabel.text = restaurantDisplayNames[indexPath.row];

    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES
     ];
}
所以当你点击一个单元格时,它会在旁边打一个复选标记。现在我希望人们能够搜索东西。问题是,苹果改变了iOS 8的搜索栏,并使其更简单,但我找不到任何关于UISearchController的教程,它取代了去润滑的方法

因此,我将搜索栏和搜索显示控制器拖放到我的视图控制器中,并添加了UISearchControllerDelegate、UISearchResultSupdated>协议声明,但我遇到了一个崩溃:

'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
每当我点击搜索栏

我也有办法

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{

}

但是它是空的,因为我不知道放什么进去。据推测,它非常简单,只需要几行代码就可以启动并运行,但我在这里找到的一个教程:now only在swift中,但没有解释该方法中的内容。

我找到了它。老兄,有些事情是如此简单可笑,但你必须深入挖掘才能找到答案。你可能会认为苹果可以在XCode中提供一些关于这些事情的提示,一些小警告或其他东西,但我是这样解决的:

显然你需要补充

if (!cell){
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
在cellForRowAtIndexPath:方法中,它如下所示:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = restaurantDisplayNames[indexPath.row];

    return cell;
}
因此,如果不存在,它会生成单元格。不知道为什么,但是哦好吧