1

我的应用程序似乎在我的搜索控制器处于活动状态时选择表格行时崩溃。当搜索控制器选择表格行时,应用程序崩溃Active

注意事项:

  • 我的搜索控制器嵌入到我的表头
  • 我现在有一个解决方法,但是,它需要我的搜索控制器时,选择表格行被解雇。这会导致较差的用户体验。我不希望有辞退我的搜索控制器

下面是我的一些代码:

class ViewProfileViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UISearchBarDelegate, UISearchResultsUpdating { 

let searchController = UISearchController(searchResultsController: nil) 

override func viewDidLoad() { 
    super.viewDidLoad() 

    searchController.searchResultsUpdater = self 
    searchController.hidesNavigationBarDuringPresentation = false 
    searchController.dimsBackgroundDuringPresentation = false 
    searchController.searchBar.sizeToFit() 
    searchController.searchBar.searchBarStyle = .minimal 
    searchController.searchBar.placeholder = "Search City" 
    searchController.searchBar.showsCancelButton = true 
    searchController.searchBar.delegate = self 
    searchController.searchBar.backgroundColor = UIColor.white 

    self.myTable.tableHeaderView = searchController.searchBar 

} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    tableView.deselectRow(at: indexPath, animated: true) 
    if searchController.isActive { 
     navigationController?.popViewController(animated: true) 
     dismiss(animated: true, completion: nil) 
    } else { 

    } 
    let mViewController = MController() 
    let navController = UINavigationController(rootViewController: mViewController) 
    present(navController,animated: true, completion: nil) 
} 

} 

回答

0

不弹出的单元选择视图控制器。无需检查searchController是否有效。

例子:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    // Get rid of searchController 
    searchController.searchBar.endEditing(true) 
    searchController.isActive = false 
    searchController.dismiss(animated: true) { /* */ } 

    // Deselect row 
    tableView.deselectRow(at: indexPath, animated: true) 

    // Present your VC here 

} 
+0

有没有提出我的VC不解雇我SearchController的方法吗?或者至少我可以在搜索控制器中保存我的搜索文本吗? – ajayb

相关问题