1

我有一个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
     ];
}

因此,当您点击一个单元格时,它会在其旁边放置一个复选标记。现在我希望人们能够搜索事物。问题是,Apple 更改了 iOS 8 的搜索栏,据说让它变得更简单,但我找不到任何关于UISearchController取代 deprecated 方法的教程。

所以我将搜索栏和搜索显示控制器拖放到我的视图控制器中并添加了协议声明,但我遇到了崩溃:UISearchControllerDelegate, UISearchResultsUpdating>

'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

每当我点击搜索栏时。

我也有方法

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

}

但它是空的,因为我不知道该放什么。据说它非常简单,只需要几行代码即可启动并运行,但我在这里找到的一个教程:http: //www.stuartbreckenridge.com/blog/examineing-the-new-uisearchcontroller-api现在只有在 swift 中,但没有解释该方法中的内容。

4

1 回答 1

0

我想到了。伙计,其中一些事情非常简单,但您必须深入挖掘才能找到答案。你会认为 Apple 可以在 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;
}

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

于 2014-12-16T23:40:42.320 回答