2017-02-09 32 views
0

我正在做一个快速的应用程序,涉及一个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

我不是Swift开发人员,所以我不确定是否应该将此标记为重复或不是,但在这里:http://stackoverflow.com/questions/31161381/cannot-invoke-filter-with-an-argument -list-of-type btw,如果你在SO上搜索你的错误代码,**许多**问题都会弹出并提供很多答案。如果你仔细观察它们,也许其中一个可以事先解决你的问题。 – 2017-02-09 02:19:28

回答

1

过滤数据的类型应该是[TableItem],与数据相同。

+0

这应该起作用。 –

+0

你能更具体一点吗?这是否解决了错误或只是一个? @ Mr.Bista – Luc

+0

两者都将得到解决。 –

相关问题