셀을 클릭하면 url로 이동하는 tableView가 포함 된 신속한 응용 프로그램을 만들고 있습니다. 내가 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()
}
}
저는 신속한 개발자가 아니므로 중복으로 표시할지 여부를 결정해야할지 모르겠습니다. http://stackoverflow.com/questions/31161381/cannot-invoke-filter-with-an-argument -list-of-type btw, SO에서 오류 코드를 검색하면 ** 많은 ** 질문에 많은 답이 표시됩니다. 당신이 그들을 통해 보면, 아마 그들 중 하나가 문제를 미리 해결할 수 있습니다. –