【问题标题】:App crashing when selecting table row while search controller Active搜索控制器活动时选择表格行时应用程序崩溃
【发布时间】:2017-10-15 20:03:26
【问题描述】:

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

注意事项:

  • 我的搜索控制器嵌入到我的表头中
  • 我目前有一个修复程序,但是,它需要在选择表行时关闭我的搜索控制器。这会导致糟糕的 UI 体验。我不想关闭我的搜索控制器

这是我的一些代码:

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)
}

}

【问题讨论】:

    标签: swift tableview segue uisearchcontroller didselectrowatindexpath


    【解决方案1】:

    不要在单元格选择时弹出视图控制器。也无需检查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
    
    }
    

    【讨论】:

    • 有什么方法可以在不关闭我的 SearchController 的情况下展示我的 VC?或者至少我可以将搜索文本保留在搜索控制器中吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多