2017-02-09 31 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

兩者都將得到解決。 –

相關問題