城市列表

城市列表上篇已经获取到了,接下来展示到table中,代码:

//
//  SearchCityViewController.swift
//  Learning
//

//

import UIKit

let SCREEN_HEIGHT = UIScreen.mainScreen().bounds.size.height
let SCREEN_WIDTH = UIScreen.mainScreen().bounds.size.width

class SearchCityViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    let reuseIdentifier = "Cell"
    var tableView:UITableView!
    var keys = [String]()
    var cities = [String: [String]]()
    var hotCities = ["北京市","上海市","广州市","深圳市","杭州市"]
    private let blankStr = "    "
    var locationCity = "正在定位"
    override func viewDidLoad() {
        super.viewDidLoad()
        createTable()
        readCities()
    }
    
    
    func readCities(){
        //生成plist的路径
        if let path = NSBundle.mainBundle().pathForResource("cityList", ofType: "plist") {
            //获取路径下的文件内容 转换成字典
            if let dic = NSMutableDictionary(contentsOfFile: path) {
                
                for (key,value) in dic {
                    if let k = key as? String {
                        keys.append(k)
                        
                        if let array = value as? [String] {
                            cities[k] = array

                        }
                    }
                }
            }
        }
        
        keys.sortInPlace()
        
        if hotCities.count > 0 {
            keys.insert("热", atIndex: 0)
            cities["热"] = hotCities
            
        }
        keys.insert("定", atIndex: 0)
        cities["定"] = [locationCity]
        
    }
    
    func createTable(){
        
        tableView = UITableView(frame: CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) ,style: .Plain)
        tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: reuseIdentifier)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.backgroundColor = UIColor.whiteColor()
        
        self.view.addSubview(tableView)
        
        
    }
    
    
    func createSectionLabel(text:String) -> UILabel {

        let label = UILabel(frame: CGRect(x:0, y:0, width:SCREEN_WIDTH, height:CGFloat(30)))
        label.text = text
        label.font = UIFont.systemFontOfSize(13)
        label.textColor = UIColor.blackColor()
        label.backgroundColor = UIColor.Gray(233)
        
        return label
    }
    
    //设置有几个section
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return keys.count
    }
    //设置右边的字母索引
    func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
        return keys
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return cities[keys[section]]!.count
    }
    
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 21
    }
    //设置header的view
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        return createSectionLabel(blankStr + (keys[section] == "热" ? "热门城市" : keys[section] == "定" ? "当前定位城市" : keys[section]))
    }

    
    // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
    // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
    
   
     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        
        let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath)
        
        cell.textLabel?.text = cities[keys[indexPath.section]]![indexPath.row]
        return cell
    }
}

 

效果:

 

posted @ 2016-01-15 17:57  mogul  阅读(208)  评论(0编辑  收藏  举报