用TableView实现分组列表展示

//
// ViewController.swift
// SimpleTable
//
// Created by Ray Zhang on 2017/10/28.
// Copyright © 2017年 Ray Zhang. All rights reserved.
//

import UIKit

class ViewController: UITableViewController,UISearchBarDelegate {

@IBOutlet weak var searchBar: UISearchBar!

var cityListDic:NSDictionary! //定义字典类型变量
var cityGroupName:NSArray!//定义城市组数组

override func viewDidLoad() {
   super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

   let cityListPath =  Bundle.main.path(forResource: "citydict", ofType: "plist")//获取文件路径
   let city = NSDictionary(contentsOfFile: cityListPath!)//获取文件内容
   self.cityListDic = city
   let tempList = cityListDic.allKeys as NSArray//获取字典所有key值
   //tempList.sortedArray(using: Selector("compare:"))//排序
   cityGroupName = tempList
   self.tableView.dataSource = self
   self.tableView.delegate = self
   self.searchBar.delegate = self
   self.searchBar.sizeToFit()
}


//UITableView数据源协议方法,定义table中节的数目。
override func numberOfSections(in tableView: UITableView) -> Int {
    return self.cityGroupName.count
}

//UITableView数据源协议方法,定义每个section的标题
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return self.cityGroupName[section] as? String
}

//UITableView数据源协议必须实现的方法,定义每个section中的行数。
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    let groupName = cityGroupName[section] as! String
    let citylist = self.cityListDic[groupName] as! NSArray
    return citylist.count
}

//UITableView数据源协议必须实现的方法,定义索引
override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
    return self.cityGroupName as? [String]
}复制代码

//UITableView数据源协议必须实现的方法,定义每个单元格现实的内容,返回Cell对象
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cellIdentifer = "cellIdentifer"
    var cell:UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: cellIdentifer)
    if (cell == nil){
        cell = UITableViewCell(style: UITableViewCellStyle.default , reuseIdentifier: cellIdentifer)
    }
    let section = indexPath.section //当前section ID
    let row = indexPath.row //该section内的row ID
    let groupName = cityGroupName[section] as! String
    let citylist = self.cityListDic[groupName] as! NSArray
    cell.textLabel?.text = citylist[row] as? String
    cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
    return cell
}

//UITableDelegate协议的方法,选中某一行时触发
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let section = indexPath.section //当前section ID
    let row = indexPath.row //该section内的row ID
    let groupName = cityGroupName[section] as! String
    let citylist = self.cityListDic[groupName] as! NSArray
    let value = citylist[row] as? String
    NSLog(value!)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}复制代码

}

转载于:https://juejin.im/post/59f68e236fb9a0451e3f109c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值