1,样例说明:
(1)列表以分组的形式展示

(2)同时还自定义分区的头部和尾部
(3)点击列表项会弹出消息框显示该项信息。

2,效果图:

     
3,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import UIKit
 
class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{
     
    var tableView:UITableView?
        
    var allnames:Dictionary<Int, [String]>?
     
    var adHeaders:[String]?
     
    override func loadView() {
        super.loadView()
    }
     
    override func viewDidLoad() {
        super.viewDidLoad()
     
        //初始化数据,这一次数据,我们放在属性列表文件里
        self.allnames =  [
            0:[String]([
            "UILabel 标签",
            "UITextField 文本框",
            "UIButton 按钮"]),
            1:[String]([
            "UIDatePiker 日期选择器",
            "UIToolbar 工具条",
            "UITableView 表格视图"])
        ];
         
        println(self.allnames)
         
        self.adHeaders = [
            "常见 UIKit 控件",
            "高级 UIKit 控件"
        ]
         
        //创建表视图
        self.tableView = UITableView(frame:self.view.frame, style:UITableViewStyle.Grouped)
        self.tableView!.delegate = self
        self.tableView!.dataSource = self
        //创建一个重用的单元格
        self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")
        self.view.addSubview(self.tableView!)
         
        //创建表头标签
        var headerLabel = UILabel(frame: CGRectMake(0, 0, self.view.bounds.size.width, 30))
        headerLabel.backgroundColor = UIColor.blackColor()
        headerLabel.textColor = UIColor.whiteColor()
        headerLabel.numberOfLines = 0
        headerLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
        headerLabel.text = "高级 UIKit 控件"
        headerLabel.font = UIFont.italicSystemFontOfSize(20)
        self.tableView!.tableHeaderView = headerLabel
    }
     
    //在本例中,有2个分区
    func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
        return 2;
    }
     
    //返回表格行数(也就是返回控件数)
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var data = self.allnames?[section]
        return data!.count
    }
     
     
    // UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的头部
    func tableView(tableView:UITableView, titleForHeaderInSection
        section:Int)->String
    {
        var headers =  self.adHeaders!;
        return headers[section];
    }
    // UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的尾部
    func tableView(tableView:UITableView, titleForFooterInSection
        section:Int)->String
    {
        var data = self.allnames?[section]
        return "有\(data!.count)个控件"
    }
     
     
    //创建各单元显示内容(创建参数indexPath指定的单元)
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
        -> UITableViewCell
    {
        //为了提供表格显示性能,已创建完成的单元需重复使用
        let identify:String = "SwiftCell"
        //同一形式的单元格重复使用,在声明时已注册
        let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath)
            as UITableViewCell
        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
         
        var secno = indexPath.section
        var data = self.allnames?[secno]
        cell.textLabel?.text = data![indexPath.row]
         
        return cell
    }
     
    // UITableViewDelegate 方法,处理列表项的选中事件
    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)
    {
        self.tableView!.deselectRowAtIndexPath(indexPath, animated: true)
         
        var itemString = self.allnames![indexPath.section]![indexPath.row]
         
        var alertview = UIAlertView();
        alertview.title = "提示!"
        alertview.message = "你选中了【\(itemString)】";
        alertview.addButtonWithTitle("确定")
        alertview.show();
         
    }
     
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
         
        // Dispose of any resources that can be recreated.
    }
}

Swift - 使用表格组件(UITableView)实现分组列表的更多相关文章

  1. 编写Java程序,使用JTable表格组件展现人员信息列表

    返回本章节 返回作业目录 需求说明: 使用JTable组件显现人员信息列表 实现思路: 创建一个JTable对象. 创建一个JScrollPane对象(显示横向和纵向滚动条). 将表格添加到滚动面板. ...

  2. Swift - 使用表格组件(UITableView)实现单列表

    1,样例说明: (1)列表内容从Controls.plist文件中读取,类型为Array. (2)点击列表项会弹出消息框显示该项信息. (3)按住列表项向左滑动,会出现删除按钮.点击删除即可删除该项. ...

  3. 【react表格组件】react-virtualized虚拟列表

    https://css-tricks.com/rendering-lists-using-react-virtualized/

  4. PropertyGird( 属性表格) 组件

    本节课重点了解 EasyUI 中 PropertyGird(属性表格)组件的使用方法,这个组件依赖于 DataGrid(数据表格)组件. 一. 加载方式 //class 加载方式<table i ...

  5. DataGrid( 数据表格) 组件[1]

    本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...

  6. Ext JS 6学习文档-第5章-表格组件(grid)

    Ext JS 6学习文档-第5章-表格组件(grid) 使用 Grid 本章将探索 Ext JS 的高级组件 grid .还将使用它帮助读者建立一个功能齐全的公司目录.本章介绍下列几点主题: 基本的 ...

  7. 第二百二十五节,jQuery EasyUI,PropertyGird(属性表格)组件

    jQuery EasyUI,PropertyGird(属性表格)组件 学习要点: 1.加载方式 2.属性列表 3.方法列表 本节课重点了解 EasyUI 中 PropertyGird(属性表格)组件的 ...

  8. 第二百二十二节,jQuery EasyUI,DataGrid(数据表格)组件

    jQuery EasyUI,DataGrid(数据表格)组件 学习要点: 1.加载方式 2.分页功能 本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于 Pa ...

  9. JS组件系列——表格组件神器:bootstrap table

    前言:之前一直在忙着各种什么效果,殊不知最基础的Bootstrap Table用法都没有涉及,罪过,罪过.今天补起来吧.上午博主由零开始自己从头到尾使用了一遍Bootstrap Table ,遇到不少 ...

随机推荐

  1. stm32之CAN发送、接收详解

    CAN接收报文并过滤之标识符过滤:(重点.难点) 在CAN协议里,报文的标识符不代表节点的地址,而是跟报文的内容相关的.因此,发送者以广播的形式把报文发送给所有的接收者.节点在接收报文时-根据标识符的 ...

  2. Search a 2D Matrix【python】

    class Solution: # @param matrix, a list of lists of integers # @param target, an integer # @return a ...

  3. PAD会取代PC吗

    公司上了新的内部管理系统,上线一片混乱,群里也很热闹,从系统是否应该支持pad谈起 ,有技术大佬说5年内就是pad时代了,pad将完全取代pc.我不知道大佬从何得出这个结论,有和数据支撑.我的观点是, ...

  4. AES加密时抛出java.security.InvalidKeyException: Illegal key size or def

    原文:AES加密时抛出java.security.InvalidKeyException: Illegal key size or def 使用AES加密时,当密钥大于128时,代码会抛出 java. ...

  5. 最小生成树(MST)[简述][模板]

    Prim(添点法) 1. 任选一点(一般选1), 作为切入点,设其与最小生成树的距离为0(实际上就是选一个点,将此树实体化),. 2. 在所有未选择的点中选出与最小生成树距离最短的, 累计其距离, 并 ...

  6. codeforces 437C The Child and Toy

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  7. android代码控制seekbar的样式

    package com.zte; import android.app.Activity; import android.graphics.Color; import android.graphics ...

  8. PHP - 接口 - 多接口

    /* * 使用多接口 */ //定义接口1 interface IPerosn_one{ public function eat(); } //定义接口2 interface IPerson_two{ ...

  9. 在WPF的DATAGRID中快速点击出现在ADDNEW或EDITITEM事务过程不允许DEFERREFRESH

    原文 在WPF的DATAGRID中快速点击出现在ADDNEW或EDITITEM事务过程不允许DEFERREFRESH 在项目中关于DataGrid的遇到过一些问题,其中是关于迁入CheckBox的双向 ...

  10. 仿Google首页搜索自动补全

    仿Google自动补全,实现细节: 后台是简单的servlet(其实就是负责后台处理数据交互的,没必要非跌用个struts...什么的) 传输介质:xml 使用jQuery js框架 功能实现: 如果 ...