uitableview - 검색 - swift4 search bar
UISearchController있는 UIViewController (3)
스위프트 (Swift)에서 애플의지도 애플리케이션과 비슷한 기능을 만들려고합니다. UISearchController를 일반보기 (즉 : UITableView가 아닌)에 통합하는 방법이 있습니까? 스토리 보드를 통해 하나를 드롭하면 연결된 검색 창 안쪽을 클릭하면 충돌이 발생합니다. 아니면 UITableView를 사용하여이 결과를 얻을 수있는 방법이 있습니까?
SearchController 구현이 너무 직관적이지 않기 때문에 ViewController의 SearchController에서 UIViewController의 SearchDisplayController를 변환하는 데 문제가있었습니다. 그러나 SearchController 자체에서 searcher를 임의의 뷰에 추가 할 수 있습니다. 집중 시키거나 선택할 때 검색 막대가 위로 움직이기 때문에 제약 조건을 설정할 수 없습니다 (보기에 추가 한 후 seachController.searchbar에 제약 조건을 설정하는 방법을 알고 있다면 알려주세요!). 아래에 ViewController에서 SearchController를 구현할 때 매우 중요하고 가치있는 체크리스트를 공유하고 있습니다.
// 모든 뷰에 searcher를 추가 할 수 있습니다. 자동으로 tableView를 마술과 같이 표시합니다. 그러나 이러한 이유 때문에 검색 막대에 추가 할 자리 표시 자 뷰에 대한 제약 조건을 설정할 수 없습니다.
[self.searchBarPlaceHolderView addSubview:self.searchController.searchBar];
// 검색 막대가 포커스 / 선택할 때 드롭 다운되지 않도록하려면이 옵션이 필요합니다. 탐색 모음의 titleview에 searchBar를 추가하는 경우이 값을 NO로 설정하려고합니다.
self.searchController.hidesNavigationBarDuringPresentation = YES;
// viewController에 이것을 설정했는지 확인하십시오.
self.extendedLayoutIncludesOpaqueBars = true;
self.definesPresentationContext = YES;
// 또한 검색 컨트롤러에 표시 할 수있는 tableViewController를 제공해야합니다. generic.properties를 사용하여 self.searchResultsController = [[UITableView alloc] init]을 수행 할 수도 있습니다.
self.searchResultsController = (UITableViewController *)[ [UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"searchResultsTableViewController"];
self.searchResultsController.tableView.dataSource = self;
self.searchResultsController.tableView.delegate = self;
self.searchResultsController.definesPresentationContext = NO;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultsController];
만약 당신이 아닌 UITableView와 함께 UISearchController를 사용하고 싶다면, 여기에 내가 그랬다.
UISearchController
는 IB가 아직 지원하지 않기 때문에 UISearchBar
처럼 아무 것도 추가 할 필요가 없습니다 .
@interface UIViewControllerSubclass () <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating>
@property (strong, nonatomic) UISearchController *searchController;
@end
@implementation UIViewControllerSubclass
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any custom init from here...
// Create a UITableViewController to present search results since the actual view controller is not a subclass of UITableViewController in this case
UITableViewController *searchResultsController = [[UITableViewController alloc] init];
// Init UISearchController with the search results controller
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
// Link the search controller
self.searchController.searchResultsUpdater = self;
// This is obviously needed because the search bar will be contained in the navigation bar
self.searchController.hidesNavigationBarDuringPresentation = NO;
// Required (?) to set place a search bar in a navigation bar
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
// This is where you set the search bar in the navigation bar, instead of using table view's header ...
self.navigationItem.titleView = self.searchController.searchBar;
// To ensure search results controller is presented in the current view controller
self.definesPresentationContext = YES;
// Setting delegates and other stuff
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
}
@end
나는 그것이 일하기 충분하다라고 생각한다 :-)
그런 다음 최소한 UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdater 메서드를 구현해야합니다.
즐겨!
UISearchController
자신을 알아 내려고. titleView
설정하는 것이 편리하지만, 내 페이지 중 하나에서 UIViewController
맨 위에 searchBar
를 두어야했습니다.
// Add a normal View into the Storyboard.
// Set constraints:
// - height: 44
// - leading and trailing so that it spans the width of the page
// - vertical position can be anywhere based on your requirements, like near the top
@IBOutlet weak var searchContainerView: UIView!
var searchResultsController = UISearchController()
override func viewDidLoad() {
// TODO: set the searchResultsController to something
let controller = UISearchController(searchResultsController: nil)
// have the search bar span the width of the screen
controller.searchBar.sizeToFit()
// add search bar to empty View
searchContainerView.addSubview(controller.searchBar)
searchResultsController = controller
}
최신 정보:
하나 또는 두 개의 프로젝트에서 UISearchController
를 구현 한 후에 검색 바를 탐색 막대에 포함시키는 @ adauguet의 접근 방식에 중점을 둡니다.
Swift의 코드는 다음과 같습니다. 한 가지 차이점은 searchResultsUpdater
이미 텍스트 변경을 수신 대기하므로 searchBar 대리자를 설정하지 않는다는 것입니다.
override func viewDidLoad() {
super.viewDidLoad()
// locationManager.delegate = self
// locationManager.desiredAccuracy = kCLLocationAccuracyBest
// locationManager.requestWhenInUseAuthorization()
// locationManager.requestLocation()
let locationSearchTable = storyboard!.instantiateViewControllerWithIdentifier("LocationSearchTable") as! LocationSearchTable
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController?.searchResultsUpdater = locationSearchTable
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search for places"
navigationItem.titleView = resultSearchController?.searchBar
resultSearchController?.hidesNavigationBarDuringPresentation = false
resultSearchController?.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
}
또한 UISearchController
를 사용하여지도 검색 결과를 표시하는 프로젝트를 처음부터 만드는 블로그 게시물을 작성했습니다. 또한 사용자 위치 가져 오기, 핀 놓기, 위치 표시를 한 줄짜리 주소로 구문 분석하기,지도 운전을 위해 Apple지도로 이동하는 설명 선 단추 만들기와 같은지도 프로젝트에서 원하는 다른 작업을 수행합니다.
http://www.thorntech.com/2016/01/how-to-search-for-location-using-apples-mapkit/
블로그 게시물은 꽤 길기 때문에 코드를 건너 뛰고 싶다면 다음과 같이 연관된 git repo를 사용하십시오.