LoginSignup
0
0

More than 1 year has passed since last update.

SearchBar付きのTableViewでセルタップ時に画面遷移させる時の注意点

Posted at

SearchBar付きのTableViewでセルをタップ時画面遷移させる時、
検索モードだと画面遷移されず詰まったので解決策を共有します。

■セルタップで画面遷移する実装

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let viewController = SelectViewController()
    let navigationViewController = UINavigationController.init(rootViewController: viewController)
    navigationViewController.modalTransitionStyle = .crossDissolve
    navigationViewController.modalPresentationStyle = .fullScreen
    self.present(navigationViewController, animated: true)
}

これだと普通にセルをタップした時は画面遷移するが
検索モードに入った状態でセルをタップしても画面遷移しなかった。

■解決方法

検索モードに入った時は、searchController.isActive = falseにする。

    if searchController.isActive {
        searchController.isActive = false
        self.present(navigationViewController, animated: true)
    } else {
        self.present(navigationViewController, animated: true)
    }

■全体のコード

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let viewController = SelectViewController()
    let navigationViewController = UINavigationController.init(rootViewController: viewController)
    navigationViewController.modalTransitionStyle = .crossDissolve
    navigationViewController.modalPresentationStyle = .fullScreen
    if searchController.isActive {
        searchController.isActive = false
        self.present(navigationViewController, animated: true)
    } else {
        self.present(navigationViewController, animated: true)
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0