swift - 对成员 'tableView(_:didSelectRowAt:)' (Swift) 的模糊引用

标签 swift uitableview uisearchbar

我正在尝试搜索显示在表格 View 中的数组,我正在关注 this教程,但是当我尝试调用 reloadData() 方法时,我得到了对成员 'tableView(_: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

}

}

最佳答案

您的tableView 缺少@IBOutlet。因此,当您引用 self.tableView 时,Swift 认为您在谈论以 tableView 开头的方法之一。

所以,去掉 searchBar(_:textDidChange:) 中的这一行:

let tableView = UITableView()

并向您的 tableView 添加一个 socket 并连接它:

@IBOutlet weak var tableView: UITableView!

关于swift - 对成员 'tableView(_:didSelectRowAt:)' (Swift) 的模糊引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50729070/

相关文章:

ios - 滚动表格 View 并单击搜索器崩溃应用程序

swift - 放下图钉并从用户在 map 上点击的位置获取地址

ios - 如何从 UITableView 中删除不需要的单元格或行

cocoa-touch - 具有 UITableViewCellStyleValue2 样式的单元格上 detailTextLabel 的宽度

ios - 删除背景颜色时使用 UISearchDisplayController 删除行 UISearchBar

objective-c - 如何在 iOS7 中更改 UISearchBar 的背景颜色

ios - 如何将分段控件添加到导航栏? (iOS)

ios - 使用 Swift 从单元格中检索图像以在 FB/Twitter 上共享

swift 错误 : cannot convert value of type 'Int32' to expected argument type 'Int32'

uitableview - 在iOS 6中使用NSLayoutConstraints制作UITableView页脚,粘在最后一节的底部和UIScrollView的底部?