代码之家  ›  专栏  ›  技术社区  ›  CristianMoisei

对成员“tableview(\u:didselectrowat:)”的引用不明确(swift)

  •  2
  • CristianMoisei  · 技术社区  · 6 年前

    我正试图搜索TableView中显示的数组,我正在跟踪 this 教程,但是当我尝试调用reloaddata()方法时 对成员“tableview(\u:didselectrowat:)”的引用不明确

    知道我遗漏了什么吗?

    提前谢谢你的建议。 这是我的listcontroller.swift代码:

    import UIKit
    
    var arrayIndex = 0
    
    class ListController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate {
    
    
        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]!
    
        @IBOutlet var searchBarOutlet: UISearchBar!
    
        //Table Delegate
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            arrayIndex = indexPath.row
            performSegue(withIdentifier: "segue", sender: self)
        }
    
        // Table Data Source
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return filteredData.count
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            // Cell Data Source
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as UITableViewCell
            cell.textLabel?.text = filteredData[indexPath.row]
    
    
            // Cell Visual Formatting
            cell.backgroundColor = UIColor(red:0.05, green:0.05, blue:0.07, alpha:0)
            cell.textLabel?.textColor = UIColor.white
            cell.textLabel?.font = UIFont(name: "Raleway", size: 18)
            cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
    
            return cell
        }
    
        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    
            filteredData = searchText.isEmpty ? data : data.filter { (item: String) -> Bool in
            return item.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil
            }
    
            let tableView = UITableView()
    
            self.tableView.reloadData()
        }
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.title = "\(titles.count) Definitions"
    
            filteredData = data
    
    }
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   rmaddy    6 年前

    你错过了 @IBOutlet 为了你 tableView . 所以当你提到 self.tableView 斯威夫特认为你所说的方法 表视图 .

    所以,别插队了 searchBar(_:textDidChange:) :

    let tableView = UITableView()
    

    并为您的 表视图 连接起来:

    @IBOutlet weak var tableView: UITableView!