標籤:
經驗教訓:很多代碼沒有必要手動敲,知道邏輯,複製也可以。
代碼一定要規範,層次要分明。
import UIKit
class ViewController: UITableViewController,UISearchBarDelegate{
//這一段沒有問題
@IBOutlet weak var searchtext: UISearchBar!
var pathdata:NSArray!
var pathfliterdata:NSMutableArray!
//這一段沒有問題
override func viewDidLoad(){
super.viewDidLoad()
self.searchtext.delegate = self
self.searchtext.showsScopeBar = false
self.searchtext.sizeToFit()
let plistpath = NSBundle.mainBundle().pathForResource("team", ofType: "plist")
self.pathdata = NSArray(contentsOfFile: plistpath!)
self.filtercontentforsearchtext("", scope:-1)
}
func filtercontentforsearchtext(searchText:NSString,scope:Int){
if(searchText.length == 0){
self.pathfliterdata = NSMutableArray(array: self.pathdata)
return
}
var tempArray :NSArray!
if(scope == 1){ //中文 name欄位是中文名
let scopepredicate = NSPredicate(format: "SELF.name contains[c] %@",searchText)
tempArray = self.pathdata.filteredArrayUsingPredicate(scopepredicate!)
self.pathfliterdata = NSMutableArray(array: tempArray)
}
else if(scope == 0){ //英文 image欄位儲存英文名
let scopepredicate = NSPredicate(format: "SELF.image contains[c] %@",searchText)
tempArray = self.pathdata.filteredArrayUsingPredicate(scopepredicate!)
self.pathfliterdata = NSMutableArray(array: tempArray)
}else { //查詢所有
self.pathfliterdata = NSMutableArray(array: self.pathdata)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.pathfliterdata.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "cellidentifier"
var cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:indexPath) as? UITableViewCell
let row = indexPath.row
let rowDict = self.self.pathfliterdata[row] as NSDictionary
cell.textLabel?.text = rowDict["name"] as? String
let imagePath = NSString(format: "%@.png", rowDict["image"] as String)
cell.imageView?.image = UIImage(named: imagePath)
return cell
}
/// 實現 UISearchBarDelegate 協議方法
// 獲得焦點,成為第一響應者
func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
self.searchtext.showsScopeBar = true
self.searchtext.sizeToFit()
return true
}
//點擊鍵盤上的搜尋按鈕
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
self.searchtext.showsScopeBar = false
self.searchtext.resignFirstResponder()
self.searchtext.sizeToFit()
}
//點擊搜尋欄取消按鈕
func searchBarCancelButtonClicked(searchBar : UISearchBar) {
//查詢所有
self.filtercontentforsearchtext("", scope:-1)
self.searchtext.showsScopeBar = false
self.searchtext.resignFirstResponder()
self.searchtext.sizeToFit()
}
//當常值內容發生改變時候調用
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
self.filtercontentforsearchtext(searchText, scope:self.searchtext.selectedScopeButtonIndex)
self.tableView.reloadData()
}
//當搜尋範圍選擇發生變化時候調用
func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
self.filtercontentforsearchtext(self.searchtext.text, scope:selectedScope)
self.tableView.reloadData()
}
}
iOS-搜尋實驗錯誤的地方-紅色部分