iOS的UITableView上的didSelectRowAt行不起作用

时间:2019-05-23 15:54:47

标签: 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()
    }
}

还有其他需要做的事情吗?该教程不会显示我缺少的任何其他代码。

2 个答案:

答案 0 :(得分:4)

您应该将UITableViewDelegate委托给您的班级,并在tableView.delegate = self函数中使用viewDidLoad。这将有助于触发UITableViewController的委托人功能。

答案 1 :(得分:0)

您可以通过两种方式进行操作:

在您的viewDidLoad()中 tableView.delegate =自我 tableView.dataSource =自我

您也可以从 Storyboard 中进行操作。将光标置于tableview上时,按ctrl键,然后将其拖到您的视图控制器中,将弹出两个选项委托和数据源。

就我而言,我必须取消选中单元格中视图的userInteractionEnabled。