ios - 为什么不能在 didSelectRowAtIndexPath 方法中关闭 View Controller

标签 ios objective-c tableview uisearchcontroller dismiss

在我的应用程序中,它有两个 View 。在第一个 ViewController有一个tableview .当第一个单元格从tableview中选择时,它会加载另一个 View 和另一个 tableview使用搜索选项。然后从 tableview 中的搜索结果中获取,在我选择一个单元格后,当前 View 应该关闭。

  • 我的第一个问题是我无法从搜索结果中直接选择单元格。我必须点击两次。
  • 我的第二个问题是不能拒绝 viewcontroller

  • 这是我的代码
    - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
    {
        NSString *enteredString = searchController.searchBar.text;
        if ([enteredString length] >= 3) {
              [self getAirportCodesFromWebService:enteredString];
        }
        [self.airportTable reloadData];
    }
    
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [airportArray count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        GetAirportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    
        GetAirport *getAirports = [airportArray objectAtIndex:indexPath.row];
        cell.AirportName.text = getAirports.AirportName;
    
        return cell;
    }
    
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        GetAirport *getAirports = [airportArray objectAtIndex:indexPath.row];
        NSString *selectedAirport = getAirports.AirportName;
        [[NSUserDefaults standardUserDefaults] setObject:selectedAirport forKey:@"selectedairport"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    
        [self dismissViewControllerAnimated:YES completion:nil];
    
    }
    

    我用了UISearchController以编程方式为 searh.inside 我根据用户输入调用 web 服务。帮我解决这个问题

    最佳答案

    首先,在您的第二个 View Controller 中,您有一个带有搜索栏的表格 View 。

  • 所以这个 TableView 有自己的委托(delegate)和数据源方法
    当您尝试在搜索栏中搜索任何内容时,搜索栏有自己的表格 View 。
  • 该表有自己的委托(delegate)和数据源方法。

  • 希望您能正确区分 tableview 委托(delegate)和数据源方法。

    从搜索 tableview 中选择任何单元格时,您应该调用
    [self.searchDisplayController setActive:NO animated:YES];
    

    这将关闭您的搜索结果 TableView 并导航到您的 Controller View 。

    关于ios - 为什么不能在 didSelectRowAtIndexPath 方法中关闭 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34853217/

    相关文章:

    ios - 用于像 Facebook 一样在墙上发帖的 Google plus API

    ios - 这个 View 可能没有收到 initWithFrame : or initWithCoder:

    ios - 如何评估 NSPredicate 中的 NSDictionary

    .net - 为 .net 商店开始移动/IOS 开发的最佳方式

    ios - TableView 的观察值

    ios - 从数组中删除特定值而不是使用索引路径删除

    swift - 奇怪的 tableView.reloadData() 行为

    android - 一种将最小化的 flutter 应用程序带到前台的方法

    ios - 滚动后,UICollectionViewCells中的AVPlayers最终会导致应用崩溃

    ios - 为 iOS 设置 Frank 时出现问题