标签:
经验教训:很多代码没有必要手动敲,知道逻辑,复制也可以。
代码一定要规范,层次要分明。
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()
}
}
标签:
原文地址:http://www.cnblogs.com/wenios/p/4818487.html