博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用TableView实现分组列表展示
阅读量:7041 次
发布时间:2019-06-28

本文共 2993 字,大约阅读时间需要 9 分钟。

//

// 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

你可能感兴趣的文章
C++新手随笔(C++中的类型转换函数)
查看>>
Wds 自定义模板部署
查看>>
初探mysql
查看>>
交换机端口的tag与untag
查看>>
查询mysql当前连接数
查看>>
我的家庭私有云计划-2
查看>>
phpExcel常用方法详解【附有php导出excel加超级链接】
查看>>
我的友情链接
查看>>
vi编辑器的简单说明
查看>>
用Java写算法之八:桶排序
查看>>
linux命令:浅谈shell中如何进行算术运算
查看>>
shell总结
查看>>
c++数据类型
查看>>
我的友情链接
查看>>
Hadoop2.7实战v1.0之Linux参数调优
查看>>
如何测试邮箱的MX解析记录是否已经生效
查看>>
五种常见的薪酬体系优缺点对比
查看>>
开篇记
查看>>
Window attributes属性详解
查看>>
js 文件上传
查看>>