0
미안 IndexPath.row 데이터를 내 TableView
에 검색 창이를 구현하는 몇 가지 문제가 내가 함께 무슨 일을하는지 알 수 없을" '(문자열) -> BOOL을'유형의 값을 변환 할 수 없습니다 예상 인수"
이 오류 메시지가 :
이 (postStruct) -> Bool
내가 여기 func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
에서이 오류 메시지가 예상 인수 유형에 유형 (String) -> Bool
의 값을 변환 할 수 없음 내 코드
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
}
}
}
감사합니다 .. 오류가 사라졌습니다. 이제 검색을하지 않는 이유가 무엇인지 알아 내야합니다. –