생성일: 2022년 1월 23일 오후 10:10
class SearchController: UITableViewController {
//MARK: - Properties
private var users = [User]()
private var filteredUsers = [User]()
private let searchController = UISearchController(searchResultsController: nil)
private var inSearchMode: Bool {
return searchController.isActive && !searchController.searchBar.text!.isEmpty
}
//MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
configureSearchController()
configureTableView()
fetchUsers()
}
... 중략 ...
//MARK: - Helpers
func configureSearchController() {
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.placeholder = "Search"
navigationItem.searchController = searchController
definesPresentationContext = false
}
}
//MARK: - UITableViewDataSource
extension SearchController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return inSearchMode ? filteredUsers.count : users.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! UserCell
let user = inSearchMode ? filteredUsers[indexPath.row] : users[indexPath.row]
cell.viewModel = UserCellViewModel(user: user)
return cell
}
}
//MARK: - UITableViewDelegate
extension SearchController {
// 다른 유저 Cell을 터치하면 해당 사용자의 프로필 화면으로 이동시키기
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let user = inSearchMode ? filteredUsers[indexPath.row] : users[indexPath.row]
let controller = ProfileController(user: user)
navigationController?.pushViewController(controller, animated: true)
}
}
//MARK: - UISearchResultsUpdating
extension SearchController: UISearchResultsUpdating {
// 사용자가 검색창에 텍스트를 입력하면 실행되는 함수 (사용자가 검색하고자 하는 이름을 받아온다)
func updateSearchResults(for searchController: UISearchController) {
guard let searchText = searchController.searchBar.text?.lowercased() else { return }
// 입력 텍스트가 users 명단에 있는지 확인, 존재한다면 filteredUsers에 추가
filteredUsers = users.filter({ $0.username.lowercased().contains(searchText) || $0.fullname.lowercased().contains(searchText) })
self.tableView.reloadData()
}
}
잘 보고 갑니다~