zoukankan      html  css  js  c++  java
  • 城市列表

    城市列表上篇已经获取到了,接下来展示到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, 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
        }
    }
    

    效果:

    I am not doing shit today !
  • 相关阅读:
    剑指offer:面试题25、二叉树中和为某值的路径
    剑指offer:面试题24、二叉搜索树的后续遍历序列
    剑指offer:面试题23、从上往下打印二叉树
    剑指offer:面试题22、栈的压入、弹出序列
    剑指offer:面试题21、包含min函数的栈
    剑指offer:面试题20、顺时针打印矩阵
    剑指offer:面试题19、二叉树的镜像
    剑指offer:面试题18、树的子结构
    剑指offer:面试题17、合并两个排序的链表
    剑指offer:面试题16、反转链表
  • 原文地址:https://www.cnblogs.com/mogul/p/5133949.html
Copyright © 2011-2022 走看看