[MapKit을 사용한 위치 검색 앱] 제대로 쓴 것 같은데 searchBar만 나오고 셀이 안나옵니다ㅠㅠㅠㅠ

1 답변 글타래를 보이고 있습니다
  • 글쓴이
    • ygc1577
      참가자
      • 글작성 : 9
      • 답글작성 : 5

      스크린샷 2022-08-09 오후 11.05.46스크린샷 2022-08-09 오후 11.07.00스크린샷 2022-08-09 오후 11.08.13위의 사진과 같이 장소등록 버튼을 누르면 옆의 검색창으로 이동한 뒤, 주소를 입력하면 주르륵 자동 완성 셀이 나오는 형식입니다

      그러나 어찌된 영문인지 셀은 코빼기도 안보입니다ㅠㅠㅠㅠㅠㅠ

      //검색창 뷰 컨트롤러인 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 에 있습니다 

      사람 한 명 살려주세요ㅠㅠㅠㅠㅠㅠ

    • TTOzzi
      참가자
      • 글작성 : 10
      • 답글작성 : 13

      첨부해주신 코드만 봤을 땐 tableViewdataSource를 설정해주지 않아서 셀이 나타나지 않는 것 같네요 🤔

      • 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 설정되어있는데도 그러네요ㅠㅠㅠㅠ

        • TTOzzi
          참가자
          • 글작성 : 10
          • 답글작성 : 13

          혹시 선언하신 tableView의 dataSource로 UITableViewDataSource 프로토콜을 채택한 객체(LocationSearchViewController)를 할당해주셨을까요?

          • ygc1577
            참가자
            • 글작성 : 9
            • 답글작성 : 5

            해결했습니다! 감사합니다!!

1 답변 글타래를 보이고 있습니다
  • 답변은 로그인 후 가능합니다.

logo landscape small

사업자번호 : 743-81-02195
통신판매업 신고번호 : 제 2022-충북청주-1278 호
고객센터 : 카카오톡채널 @yagom