- This topic has 4개 답변, 2명 참여, and was last updated 2 years, 3 months 전에 by ygc1577.
-
글쓴이글
-
-
ygc1577참가자
- 글작성 : 9
- 답글작성 : 5
위의 사진과 같이 장소등록 버튼을 누르면 옆의 검색창으로 이동한 뒤, 주소를 입력하면 주르륵 자동 완성 셀이 나오는 형식입니다
그러나 어찌된 영문인지 셀은 코빼기도 안보입니다ㅠㅠㅠㅠㅠㅠ
//검색창 뷰 컨트롤러인 LocationSearchViewController.swift파일
import UIKit
import MapKit
protocol LocationSearchViewControllerDelegate: AnyObject {
func sendData(location: MKMapItem)
}
class LocationSearchViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchbar: UISearchBar!
private var searchCompleter: MKLocalSearchCompleter?
private var searchRegion: MKCoordinateRegion = MKCoordinateRegion(MKMapRect.world)
var completerResults: [MKLocalSearchCompletion]?
private var places: MKMapItem? {
didSet {
tableView.reloadData()
}
}
private var localSearch: MKLocalSearch? {
willSet {
places = nil
localSearch?.cancel()
}
}
weak var delegate: LocationSearchViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
searchCompleter = MKLocalSearchCompleter()
searchCompleter?.delegate = self
searchCompleter?.resultTypes = .address
searchCompleter?.region = searchRegion
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
searchCompleter = nil
}
private func search(for suggestedCompletion: MKLocalSearchCompletion) {
let searchRequest = MKLocalSearch.Request(completion: suggestedCompletion)
search(using: searchRequest)
}
private func search(using searchRequest: MKLocalSearch.Request) {
searchRequest.region = searchRegion
searchRequest.resultTypes = .pointOfInterest
localSearch = MKLocalSearch(request: searchRequest)
localSearch?.start { [unowned self] (response, error) in
guard error == nil else {
return
}
self.places = response?.mapItems[0]
print(places?.placemark.coordinate as Any)
self.delegate?.sendData(location: places!) // 여기 수정 가능성
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
}
}
extension LocationSearchViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText == “” {
completerResults = nil
}
searchCompleter?.queryFragment = searchText
}
}
extension LocationSearchViewController: MKLocalSearchCompleterDelegate {
func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
completerResults = completer.results
tableView.reloadData()
}
func completer(_ completer: MKLocalSearchCompleter, didFailWithError error: Error) {
if let error = error as NSError? {
print(“MKLocalSearchCompleter encountered an error: (error.localizedDescription). The query fragment is: \”(completer.queryFragment)\””)
}
}
}
extension LocationSearchViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if let suggestion = completerResults?[indexPath.row] {
search(for: suggestion)
}
}
}
extension LocationSearchViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return completerResults?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: “Cell”) as? Cell else { return UITableViewCell() }
if let suggestion = completerResults?[indexPath.row] {
cell.titleLabel.text = suggestion.title
cell.subtitleLabel.text = suggestion.subtitle
}
return cell
}
}
//
//아래는 커스텀 Cell.swift 파일
import UIKit
class Cell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var subtitleLabel: UILabel!
}
하다만 실제 파일은 여기 https://github.com/jinyongyun/Waudiya 에 있습니다
사람 한 명 살려주세요ㅠㅠㅠㅠㅠㅠ
2022-08-09 오후 11:18 #55320 -
-
ygc1577참가자
- 글작성 : 9
- 답글작성 : 5
extension LocationSearchViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return completerResults?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: “Cell”) as? Cell else { return UITableViewCell() }
if let suggestion = completerResults?[indexPath.row] {
cell.titleLabel.text = suggestion.title
cell.subtitleLabel.text = suggestion.subtitle
}
return cell
}
이렇게 extension으로 dataSource 설정되어있는데도 그러네요ㅠㅠㅠㅠ
2022-08-10 오전 9:33 #55382
-
-
-
글쓴이글
- 답변은 로그인 후 가능합니다.