我有一個UITableViewCell
與3子視圖,我想過濾的時候我搜索,如下所示:UISearchController過濾斯威夫特
1.圖像視圖 2.名稱標籤(黑色文本) 3.街道名稱標籤(藍色文本)
這是我迄今所做的,我只設法瞭解如何篩選1個陣列的名稱是:
MainTableView.swift
var FilteredNames = [String]
func updateSearchResultsForSearchController(searchController:UISearchController) {
// Filter Names
self.filteredNames = self.names.filter { (name:String) -> Bool in
if name.lowercaseString.containsString(self.searchController.searchBar.text!.lowercaseString){
return true
} else {
return false
}
}
self.resultsController.tableView.reloadData()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100.5
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Search
if tableView == self.tableView {
self.streets.count
return self.names.count
} else {
self.filteredStreets.count
return self.filteredNames.count
}
// return names.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = self.tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
if tableView == self.tableView {
cell.photo.image = self.images[indexPath.row]
cell.name.text = names[indexPath.row]
cell.streetName.text = streets[indexPath.row]
} else {
cell.photo.image = self.images[indexPath.row]
cell.name.text = self.filteredNames[indexPath.row]
cell.streetName.text = self.streets[indexPath.row]
}
return cell
}
至於現在,當我搜索,圖像視圖&街道標籤不與名稱保持同步。我想過濾所有3個子視圖以正確同步。我怎樣才能做到這一點?
我明白我需要使用結構,並用一個物體過濾所有3,但心中已經遇到了一些困難,管理要做到這一點任何幫助將不勝感激謝謝!
編輯:這裏是我的代碼現在:
override func viewDidLoad() {
super.viewDidLoad()
var searchController : UISearchController!
var resultsController = UITableViewController()
// //
definesPresentationContext = true
self.resultsController.tableView.dataSource = self
self.resultsController.tableView.delegate = self
self.searchController = UISearchController(searchResultsController: self.resultsController)
self.tableView.tableHeaderView = self.searchController.searchBar
self.searchController.searchResultsUpdater = self
self.searchController.dimsBackgroundDuringPresentation = true
self.searchController.searchBar.sizeToFit()
self.searchController.searchBar.barTintColor = UIColor.blackColor()
self.searchController.searchBar.endEditing(true)
self.searchController.searchBar.placeholder = "חפש ברים"
allUsers = createUsers(names: names, streets: streets, images: images)
filteredUsers = allUsers
}
var allUsers: [User]!
var filteredUsers: [User]!
func createUsers(names names: [String], streets: [String], images: [UIImage?]) -> [User] {
var users = [User]()
guard names.count == streets.count && names.count == images.count else { return users }
for (index, name) in names.enumerate() {
let user = User(name: name, streetName: streets[index], image: images[index])
users.append(user)
}
return users
}
//MARK : Search !
func updateSearchResultsForSearchController(searchController:UISearchController) {
if let searchText = searchController.searchBar.text?.lowercaseString {
if searchText.characters.count == 0 {
filteredUsers = allUsers
}
else {
filteredUsers = allUsers.filter {
return $0.name.lowercaseString.containsString(searchText) ||
$0.streetName.lowercaseString.containsString(searchText)
}
}
}
self.resultsController.tableView.reloadData()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return 100.5;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
////////
////////
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredUsers.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = self.tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
let user = filteredUsers[indexPath.row]
cell.photo.image = user.image
cell.name.text = user.name
cell.streetName.text = user.streetName
return cell
}
從[此時此刻]啓動(https://www.youtube.com/watch?v=SgEO7nni5CQ&feature= youtu.be&t = 2318)看幾分鐘斯坦福大學課程。 ** H ** e使用'didSet' +'reloadData'。我認爲這有助於解決您的問題 – Honey