2016-04-26 13 views
1

jsonファイルの検索結果で検索フィルタを作成しようとしています。UIViewController内でUITableViewを使用した検索バー

ので、私は私のクラスでUISearchResultsUpdatingを挿入しようとしているが、私はメンバーではないエラーを取得し、次のコード

import UIKit 
import Alamofire 
import SwiftyJSON 



class checkViewController: UIViewController, UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate{ 



    @IBOutlet weak var searchBar: UISearchBar! 
    @IBOutlet weak var checkTable: UITableView! 


    var arrRes = [[String:AnyObject]]() 

    var filteredData = [[String:AnyObject]]() 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     checkTable.dataSource = self 
     searchBar.delegate = self 
     filteredData = arrRes 



     Alamofire.request(.GET, "https://example.com/search", parameters: ["q": "", "type": "name"]) 
      .responseJSON { (responseData) -> Void in 
       if((responseData.result.value) != nil) { 
        let swiftyJsonVar = JSON(responseData.result.value!) 
        print(swiftyJsonVar) 
        if let resData = swiftyJsonVar["data"].arrayObject { 
         self.arrRes = resData as! [[String:AnyObject]] 
        } 
        if self.arrRes.count > 0 { 
         self.checkTable.reloadData() 
        } 
       }     
     }  
    }   
    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    }   
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {   
      return arrRes.count   
    }  
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("checkInCellView", forIndexPath: indexPath) 
     var dict = arrRes[indexPath.row] 
     cell.textLabel?.text = dict["name"] as? String 
     return cell 
    } 


} 

を持っています。だから私はUISearchBarDelegateにしようとしているが、私は唯一のために[文字列]の配列の方法を見つけることができず、[[文字列:ANYOBJECT]]この例で

class ViewController: UIViewController, UITableViewDataSource, UISearchBarDelegate { 
    @IBOutlet weak var tableView: UITableView! 
    @IBOutlet weak var searchBar: UISearchBar! 

    let data = ["New York, NY", "Los Angeles, CA", "Chicago, IL", "Houston, TX", 
     "Philadelphia, PA", "Phoenix, AZ", "San Diego, CA", "San Antonio, TX", 
     "Dallas, TX", "Detroit, MI", "San Jose, CA", "Indianapolis, IN", 
     "Jacksonville, FL", "San Francisco, CA", "Columbus, OH", "Austin, TX", 
     "Memphis, TN", "Baltimore, MD", "Charlotte, ND", "Fort Worth, TX"] 

    var filteredData: [String]! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView.dataSource = self 
     searchBar.delegate = self 
     filteredData = data 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("TableCell") as UITableViewCell 
     cell.textLabel?.text = filteredData[indexPath.row] 
     return cell 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return filteredData.count 
    } 

    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { 
     filteredData = searchText.isEmpty ? data : data.filter({(dataString: String) -> Bool in 
      return dataString.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil 
     }) 

    } 

    // This method updates filteredData based on the text in the Search Box 
    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { 
     // When there is no text, filteredData is the same as the original data 
     if searchText.isEmpty { 
      filteredData = data 
     } else { 
      // The user has entered text into the search box 
      // Use the filter method to iterate over all items in the data array 
      // For each item, return true if the item should be included and false if the 
      // item should NOT be included 
      filteredData = data.filter({(dataItem: String) -> Bool in 
       // If dataItem matches the searchText, return true to include it 
       if dataItem.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil { 
        return true 
       } else { 
        return false 
       } 
      }) 
     } 
     tableView.reloadData() 
    } 
} 

はのためにも、それを作るための方法はあります[[String:AnyObject]] ???

私はこれを実行しようとするので、私は、エラータイプの

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { 
    filteredData = searchText.isEmpty ? arrRes : arrRes.filter({(dataString: [String:AnyObject]) -> Bool in 
     return dataString.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil 
    }) 

} 

値を取得する「[文字列:ANYOBJECT]」

これに対処するためにどのように任意のアイデア「をrangeOfString」はメンバーを持っていませんか?

答えて

1

希望のコードは以下のとおりです。

override func viewDidLoad() 
{ 
    super.viewDidLoad() 

    var MyStringArray: [String] = ["ABC","XYZ","SomeData"] 

    let strDemo : String = "SomeData" 

    for indexNo in 0..<MyStringArray.count 
    { 

     if MyStringArray[indexNo].rangeOfString(strDemo) != nil 
     { 
      print("found") 
     } 
     else 
     { 
      print("Not found") 
     } 
    } 

} 
関連する問題