iOS-搜尋實驗錯誤的地方-紅色部分

來源:互聯網
上載者:User

標籤:

 

經驗教訓:很多代碼沒有必要手動敲,知道邏輯,複製也可以。

代碼一定要規範,層次要分明。

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-搜尋實驗錯誤的地方-紅色部分

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.