【发布时间】:2017-03-18 20:40:40
【问题描述】:
我在使用 IndexPath.row 数据为我的 TableView 实现 SearchBar 时遇到了一些问题
我不知道如何处理此错误消息:
无法将 (String) -> Bool 类型的值转换为预期的参数类型 (postStruct) -> Bool
我在func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) 中收到此错误消息
这是我的代码
import UIKit
import Firebase
import FirebaseDatabase
struct postStruct {
let username : String!
let school : String!
}
class TableViewProfileController: UITableViewController, UISearchBarDelegate {
@IBOutlet weak var searchBar: UISearchBar!
var posts:[postStruct] = []
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Find friends"
searchBar.delegate = self
tableView.dataSource = self
//Hide backButton in Navigationbar
self.navigationItem.setHidesBackButton(true, animated: false)
// SearchController
searchBar.sizeToFit()
searchBar.searchBarStyle = .minimal
searchBar.placeholder = "Search here..."
searchBar.setShowsCancelButton(false, animated: true)
searchBar.keyboardAppearance = .default
searchBar.tintColor = UIColor(red: 38.0/255.0, green: 68.0/255.0, blue: 102.0/255.0, alpha: 1.0)
self.definesPresentationContext = true
self.tableView.reloadData()
// TableView Cell Content
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("users").queryOrderedByKey().observe(.childAdded, with: {
snapshot in
let snapshotValue = snapshot.value as? NSDictionary
let username = snapshotValue!["Username"] as! String
let school = snapshotValue!["School"] as! String
self.posts.insert(postStruct(username: username, school: school) , at: 0)
self.tableView.reloadData()
})
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
posts = searchText.isEmpty ? posts : posts.filter({(dataString: String) -> Bool in
return dataString.range(of: searchText, options: .caseInsensitive) != nil
})
tableView.reloadData()
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.searchBar.showsCancelButton = true
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.showsCancelButton = false
searchBar.text = ""
searchBar.resignFirstResponder()
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 2.0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
let label1 = cell?.viewWithTag(1) as! UILabel
label1.text = posts[indexPath.row].username
label1.font = UIFont(name: "Arial Rounded MT Bold", size: 16)
label1.textColor = UIColor.black
let label2 = cell?.viewWithTag(2) as! UILabel
label2.text = posts[indexPath.row].school
label2.font = UIFont(name: "Arial", size: 11)
label2.textColor = UIColor(red: 102.0/255.0, green: 102.0/255.0, blue: 102.0/255.0, alpha: 1.0)
cell?.imageView?.image = UIImage(named: "Userlogo")
return cell!
}
//Preparing Segue to show username in FindUserViewController
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "findSegue", sender: self.posts[indexPath.row].username)
}
//Segue to show username in FindUserViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier! == "findSegue" {
let guest = segue.destination as! FindUserViewController
guest.pickedUser = sender as! String
let backItem = UIBarButtonItem()
backItem.title = "Back"
navigationItem.backBarButtonItem = backItem
}
}
}
【问题讨论】:
标签: ios swift uitableview uisearchbar