UITableView 上的 iOS didSelectRowAt 行不起作用

标签 ios swift uitableview didselectrowatindexpath

我正在尝试跟进有关 CodePath 的本教程:访问 https://guides.codepath.com/ios/Search-Bar-Guide#cancelling-out-of-search-and-hiding-keyboard

我创建了一个 SearchViewController 来进行搜索,但是 didSelectRowAt 不起作用

下面是我的代码:

class SearchViewController: UIViewController, UITableViewDataSource, UISearchBarDelegate {
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var searchBar: UISearchBar!

    let data = ["New York, NY", "Los Angeles, CA", "Chicago, IL", "Houston, TX",
                "Philadelphia, PA", "Phoenix, AZ", "San Diego, CA", "San Antonio, TX",
                "Dallas, TX", "Detroit, MI", "San Jose, CA", "Indianapolis, IN",
                "Jacksonville, FL", "San Francisco, CA", "Columbus, OH", "Austin, TX",
                "Memphis, TN", "Baltimore, MD", "Charlotte, ND", "Fort Worth, TX"]

    var filteredData: [String]!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.allowsSelection = true
        searchBar.delegate = self
        filteredData = data
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as UITableViewCell
        cell.textLabel?.text = filteredData[indexPath.row]
        return cell
    }

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


    // THIS IS NOT WORKING
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("didSelectRowAt(\(indexPath)")
    }

    // This method updates filteredData based on the text in the Search Box
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        // When there is no text, filteredData is the same as the original data
        // When user has entered text into the search box
        // Use the filter method to iterate over all items in the data array
        // For each item, return true if the item should be included and false if the
        // item should NOT be included
        filteredData = searchText.isEmpty ? data : data!.filter { (item: String) -> Bool in
            // If dataItem matches the searchText, return true to include it
            return item.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil
        }

        tableView.reloadData()
    }

    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        self.searchBar.showsCancelButton = true
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchBar.showsCancelButton = false
        searchBar.text = ""
        searchBar.resignFirstResponder()
    }
}

我还需要做些什么吗?本教程没有显示我遗漏的任何其他代码。

最佳答案

您应该将 UITableViewDelegate 委托(delegate)给您的类,并在您的 viewDidLoad 函数中使用 tableView.delegate = self。这将有助于触发 UITableViewController 的委托(delegate)函数。

关于UITableView 上的 iOS didSelectRowAt 行不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56278966/

相关文章:

ios - UIToolbarButton 转至 UITabBarController 中的特定 View

ios - 在 Ios 中合并两个视频

ios - 实例成员不能用于类型 - 错误

swift - 如何将类型应用于 NSFetchRequest 实例?

objective-c - 如何点击TableViewCell打开一个新 View 并保留数据?

ios - 如何将 ios esri arcGIS 点转换为纬度和经度

ios - 将程序化的 UIView 发送到 Storyboard 中添加的 UIView

ios - 在代码片段示例中使用__autoreleasing

ios - 使用swift 4将字典传递到 TableView 中

ios - setIsAccessibilityElement 在 UITableViewCell 中不起作用