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 !
  • 相关阅读:
    【华为云技术分享】使用keil5打开GD32F450i的MDK项目出现的问题以及J-Link无法烧录程序对应的解决方案
    【华为云技术分享】不为人知的稠密特征加入CTR预估模型的方法
    205. 判断两个字符串的模式是否相同 Isomorphic Strings
    541. 反转字符串2 Reverse String II
    插入排序,二分查找插入排序,使用二叉树的插入排序
    二分查找,求mid值的类型溢出问题
    二叉搜索树类的C#实现
    二叉搜索树,删除节点
    二叉搜索树的前驱节点和后继节点
    438. 匹配字符串(顺序不同但个数相同的字符串) Find All Anagrams in a String
  • 原文地址:https://www.cnblogs.com/mogul/p/5133949.html
Copyright © 2011-2022 走看看