gpt4 book ai didi

ios - DidSelectRowAt 不会返回所需的项目

转载 作者:行者123 更新时间:2023-11-29 05:44:40 25 4
gpt4 key购买 nike

我有一个动态表格,其中显示许多 MKMapItems - 这些项目由 cellForRowAt 显示。但不知何故,如果我点击单元格 didSelectRow 如果我想打印它,将不会返回 map 项:

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)
}
}
}

最佳答案

guard let mapItem =places?[indexPath.row] else { return }- 你确定这个守卫声明不会返回吗?

另外,我没有看到您在哪里将 VC 设置为 tableView 委托(delegate)/数据源。

关于ios - DidSelectRowAt 不会返回所需的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56259391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com