UIKit에서는 Search bar를 만드는 방법은 크게 두가지가 있다.
- Search bar
- SearchConroller
그렇다면 두개의 차이는 무었일까?????????????
UISearchBar

search bar는 말 그대로 서치바 이다.
UISearchBar
provides a text field for entering text, a search button, a bookmark button, and a cancel button. A search bar doesn’t actually perform any searches. You use a delegate, an object conforming to theUISearchBarDelegate
protocol, to implement the actions when the user enters text or clicks buttons.
서치바는, searchbarDelegate를 이용해서 텍스트가 입력되거나 버튼이눌렸을때 어떠한 액션을 취할것인지 정의하고 검색하게 된다.
UISearchController

UISearchController는 새로운 뷰 컨트롤러에 검색 결과를 보여주는 상호작용을 할때 사용하는 컨트롤러이다.
- 즉 UISearchBar와의 큰 차이점은 “결과를 보여주는가 아닌가” 에 대한 것이다.
그래서 UISearchController에는 searchResultController가 존재하게 된다.
이런식의 구조이다.

사용하는 법
사실좀 많이 헤매었는데, 사실 개념만 알고나면 굉장히 간단하다.
1. UISearchController를 생성한다.

2. navigationItem에 추가한다.

3. 검색이 시작될때, 사용될 searchResultController를 생성한다.
저는 테이블 뷰로 하였습니다.
TableViewController로 만들면 좀 더 편함.

4. TableViewCell을 선언한다.
개인적으로 Nib파일 없이 하는게 훨신 편하기 때문에 그냥 저기 코드에
static let identifier = “TableViewCell” 추가해서 사용하는게 편함.

5. 서치바를 사용할 뷰에 UISearchResultsUpdating 프로토콜 채택


그리고 viewdidLoad에 searchResultsUpdater를 채택해준다.
UISearchResultsUpdating이란?



해당 프로토콜을 채택하면 안에 있는 updateSearchResults 메소드를 구현해야하는데, 이 함수는 searchbar에 입력이 들어오면 실행되는 함수이다.
가장중요한!! searchResultsController란?
searchResultsController란, searchBar에 입력이 들어왔을 때, 결과로 생성할 컨트롤러를 의미한다.
가장 중요한것은 “입력이 들어왔을 때 생성한다는것이다. “
Uploaded by N2T