SearchBar在TableView中

时间:2016-02-23 07:10:42

标签: ios swift uitableview

你好我试图在我的应用程序中实现SearchBar。我已经按照一些教程并成功实现了SearchBar。但问题是,在我的工作代码中,数据是硬编码的,并且位于NSArray,而在我的实际应用中,数据位于NSDictionary来自Web服务。这是工作代码。我想用Nsdictionary

实现这个
class  CountryTableViewController: UITableViewController, UISearchResultsUpdating {

    var filterTableData = [String]()
    var resultSearchController = UISearchController()

    var tableData = ["one","two","three"]



    override func viewDidLoad() {
        super.viewDidLoad()



        self.resultSearchController = ({

            let controller  = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self
            controller.dimsBackgroundDuringPresentation = false
            controller.searchBar.sizeToFit()
            self.tableView.tableHeaderView = controller.searchBar
            return controller


        })()

        self.tableView.reloadData()
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


        if(self.resultSearchController.active){

            return self.filterTableData.count
        }else {
            return tableData.count
        }



    }



    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CountryTableViewCell

        if(self.resultSearchController.active){


            cell.countryNameLabel.text = filterTableData[indexPath.row]
            return cell

        }else{

            cell.countryNameLabel.text = tableData[indexPath.row]
            return cell
        }


    }

    func updateSearchResultsForSearchController(searchController: UISearchController) {
        filterTableData.removeAll(keepCapacity: false)

        let searchPredict = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)

        let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredict)
        filterTableData = array as! [String]
        self.tableView.reloadData()
    }
  }

我的NSDictionary数据就像这样

{

    0 =     {

        City =         {

            code = 11859;

            "country_id" = 177;

            "created_at" = "<null>";

            id = 23219;

            name = Lahore;



        };

        Country =         {

            id = 177;

            name = "Pakistan

\n";

        };

        State =         {

            Country =             (

            );

            id = 3262;

            name = "Punjab

";

        };

    };

    code = 200;

}

我尝试过使用Nsdictionary但没有成功

1 个答案:

答案 0 :(得分:0)

您可以使用此

NSDictionary *dictn = [main_dictn objectForKey:@"0"];
NSString *str = [NSString stringWithFormat:@"%@ , %@",[[dictn objectForKey:@"City"] objectForKey:@"name"],[[dictn objectForKey:@"Country"] objectForKey:@"name"]];

在Swift中

let dictn : NSDictionary = main_dictn.objectForKey("0") as! NSDictionary

let Cityname = dictn["City"]?.objectForKey("name") as! String /// get city name
let Countryname = dictn["Country"]?.objectForKey("name") as! String /// get country name

let str : String = String(format: "%@ , %@",Cityname,Countryname) 
相关问题