DidSelectRowAt将不返回所需的项目

时间:2019-05-22 14:40:47

标签: ios swift

我有一个动态表,其中显示了多个MKMapItems-这些项正由cellForRowAt显示。但是以某种方式,如果我单击单元格didSelectRow如果要打印它,将不会返回地图项:

class SearchResultTableViewController: UIViewController {

    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var visualEffectView: UIVisualEffectView!

    private enum SegueID: String {
        case showDetail
        case showAll
    }

    private enum CellReuseID: String {
        case resultCell
    }

    private var places: [MKMapItem]? {
        didSet {
            tableView.reloadData()
        }
    }

    private var suggestionController: SuggestionsTableTableViewController!
    var searchController: UISearchController!


    private var localSearch: MKLocalSearch? {
        willSet {
            places = nil
            localSearch?.cancel()
        }
    }

    private var boundingRegion: MKCoordinateRegion?

    override func awakeFromNib() {
        super.awakeFromNib()

       ...
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        ...
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        if #available(iOS 10, *) {
            visualEffectView.layer.cornerRadius = 9.0
            visualEffectView.clipsToBounds = true
        }
    }

    ...
}

extension SearchResultTableViewController: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return places?.count ?? 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: CellReuseID.resultCell.rawValue, for: indexPath)

        if let mapItem = places?[indexPath.row] {
            cell.textLabel?.text = mapItem.name
            cell.detailTextLabel?.text = mapItem.placemark.formattedAddress
        }

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        print(indexPath)

        ...

        guard let mapItem = places?[indexPath.row] else { return }
        print(mapItem.name)

        ...

        if tableView == suggestionController.tableView, let suggestion = suggestionController.completerResults?[indexPath.row] {
            searchController.isActive = false
            searchController.searchBar.text = suggestion.title
            search(for: suggestion)
        }
    }
}

1 个答案:

答案 0 :(得分:0)

guard let mapItem = places?[indexPath.row] else { return } -您确定此保护声明不会返回吗?

另外,我看不到将VC设置为tableView委托/数据源的位置。