0

I am making a swift app that involves a tableView that goes to a url when you click the cell. I implemented search inside of the tableview but I am getting two errors inside of the SearchBar code.

Here is my code.

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()
    }
}
Umair Afzal
  • 4,947
  • 5
  • 25
  • 50
Luc
  • 53
  • 6
  • 1
    I am not a Swift developer so I am not sure if I should flag this as a duplicate or not but here: http://stackoverflow.com/questions/31161381/cannot-invoke-filter-with-an-argument-list-of-type btw, if you search for your errorcode on SO , **many** questions will pop up with many answers. If you look through them , maybe one of them can solve your issue beforehand. –  Feb 09 '17 at 02:19

1 Answers1

1

The type of filteredData should be [TableItem], same as data.

jia ma
  • 198
  • 5
  • Can you be a little bit more specific? Does this solve both the errors or just one? @Mr.Bista – Luc Feb 09 '17 at 04:19
  • So what line of code specifically should I edit to resolve the errors? @Mr.Bista – Luc Feb 09 '17 at 04:36
  • 1
    Change `var filteredData: [String]!` to `var filteredData: [TableItem]!` – Bista Feb 09 '17 at 04:37
  • I implemented this and the errors went away but as I put text into the SearchBar it did not filter. Have any Idea of why it's doing that? @Mr.Bista – Luc Feb 09 '17 at 04:48
  • Use filteredData in function numberOfRowsInSection, cellForRowAt. @Luc – jia ma Feb 09 '17 at 05:50