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

相關問題