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 !
  • 相关阅读:
    Linux下Rootkit的另类检测
    用iptables抗御SYN Flood攻击
    用iptables抗御SYN Flood攻击
    突破极限 解决大硬盘上安装Sco Unix新思路
    突破极限 解决大硬盘上安装Sco Unix新思路
    安装、配置Vmware Esx Server 3.5视频全过程
    安装、配置Vmware Esx Server 3.5视频全过程
    应该如何对企业局域网性能传输进行测试分析
    Leetcode-944 Delete Columns to Make Sorted(删除列以使之有序)
    Leetcode-941 Valid Mountain Array(有效的山脉数组)
  • 原文地址:https://www.cnblogs.com/mogul/p/5133949.html
Copyright © 2011-2022 走看看