不能指定值类型

时间:2017-02-09 01:21:43

标签: ios swift uitableview uisearchbar uisearchbardelegate

我正在创建一个快速的应用程序,它涉及一个tableView,当你单击该单元格时,它会转到url。我在tableview中实现了搜索,但是我在SearchBar代码中遇到了两个错误。

这是我的代码。

import Foundation
import UIKit

class ViewControllerAnalysis: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {

    @IBOutlet weak var searchBar: UISearchBar!

    @IBOutlet weak var tableview: UITableView!

    struct TableItem {
        let url: String
        let name: String
        let symbol: String
    }

    let data = [
        TableItem(
            url: "https://www.zacks.com/stock/quote/AAPL?q=aapl?q=AAPL?q=AAPL",
            name: "Apple Inc.",
            symbol: "AAPL"
        ),
        TableItem(
            url: "https://www.zacks.com/stock/quote/GOOG?q=goog?q=GOOG?q=GOOG",
            name: "Google Inc.",
            symbol: "GOOG"
        ),
        TableItem(
            url: "https://www.zacks.com/stock/quote/FB?q=fb?q=FB?q=FB",
            name: "Facebook Inc.",
            symbol: "FB"
        ),
        TableItem(
            url: "https://www.zacks.com/stock/quote/AMZN?q=amzn?q=AMZN?q=AMZN",
            name: "Amazon",
            symbol: "AMZN"
        )
    ]

    let cellIdentifier = "Cell"

    var filteredData: [String]!

    override func viewDidLoad() {
        super.viewDidLoad()

        var filteredData = data

        self.tableview.dataSource = self
        self.tableview.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! StockTableViewCell

        let item = data[indexPath.row]cell.stockNameLabel?.text = item.name
        cell.stockSymbolLabel?.text = item.symbol

        return cell
    }



    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = data[indexPath.row]
        let url = URL(string: item.url)
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url!, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(url!)
        }
    }

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchText.isEmpty {
            filteredData = data
            Here is where I am getting "Cannot Assign Value Type"^^^
            On the filteredData = Data
        } else {
            filteredData = data.filter { $0.name.range(of: searchText, options: .caseInsensitive) != nil }
            Then right here I am getting "Cannot invoke 'filter' with an argument list of type."^^^
        }
        tableview.reloadData()
    }

    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        searchBar.showsCancelButton = true

    }

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

1 个答案:

答案 0 :(得分:1)

filteredData的类型应为[TableItem],与数据相同。

相关问题