UITableView具有var tableHeaderView:UIView?属性和var tableFooterView:UIView?属性,可以通过给其赋值来创建列表TableView的页眉和页脚。

效果图如下:
代码如下:
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
import UIKit
 
class ViewController: UIViewController,UITableViewDelegate,
    UITableViewDataSource,UIGestureRecognizerDelegate {
     
    var tableView:UITableView?
     
    var ctrlnames:[String] = ["UILabel 标签","UIButton 按钮","UIDatePiker 日期选择器"]
     
    override func viewDidLoad() {
        super.viewDidLoad()
         
        //创建表视图
        self.tableView = UITableView(frame: UIScreen.mainScreen().applicationFrame,
            style:UITableViewStyle.Plain)
        self.tableView!.delegate = self
        self.tableView!.dataSource = self
        //创建一个重用的单元格
        self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")
        self.view.addSubview(self.tableView!)
         
        //给TableView添加表头页眉
        var headerView:UIView = UIView(frame: CGRectMake(0,0,tableView!.frame.size.width,60))
        var headerlabel:UILabel = UILabel(frame: headerView.bounds)
        headerlabel.textColor = UIColor.whiteColor()
        headerlabel.backgroundColor = UIColor.clearColor()
        headerlabel.font = UIFont.systemFontOfSize(16)
        headerlabel.text = "TableView 页眉"
        headerView.addSubview(headerlabel)
        headerView.backgroundColor = UIColor.blackColor()
        tableView?.tableHeaderView = headerView
         
        //给TableView添加表头页尾
        var footerView:UIView = UIView(frame: CGRectMake(0,0,tableView!.frame.size.width,60))
        var footerlabel:UILabel = UILabel(frame: footerView.bounds)
        footerlabel.textColor = UIColor.whiteColor()
        footerlabel.backgroundColor = UIColor.clearColor()
        footerlabel.font = UIFont.systemFontOfSize(16)
        footerlabel.text = "TableView 页眉"
        footerView.addSubview(footerlabel)
        footerView.backgroundColor = UIColor.blackColor()
        tableView?.tableFooterView = footerView
    }
     
    //在本例中,只有一个分区
    func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
        return 1;
    }
     
    //返回表格行数(也就是返回控件数)
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.ctrlnames.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
        cell.textLabel?.text = self.ctrlnames[indexPath.row]
        return cell
    }
}

Swift - 给表格TableView添加页眉和页脚的更多相关文章

  1. iOS开发——UI_swift篇&TableView实现页眉和页脚

    TableView实现页眉和页脚   在UItableView中header和footer是很常见的,而且他能让你实现很复杂的功能,我们见过最多的就是下拉刷新和上啦加载更多,当然你还可以在上面添加一个 ...

  2. openxml(二) 添加页眉,页脚

    openxml 中 word 文档的结构是如下图: 其中,页眉是 header,属于headerpart 部件,页脚是footer,属于footerpart 部件,图上还有其他的东西,之后会一一介绍. ...

  3. C# 操作Word 文档——添加Word页眉、页脚和页码

    在Word文档中,我们可以通过添加页眉.页脚的方式来丰富文档内容.添加页眉.页脚时,可以添加时间.日期.文档标题,文档引用信息.页码.内容解释.图片/LOGO等多种图文信息.同时也可根据需要调整文字或 ...

  4. C# 添加Word页眉、页脚和页码

    在Word文档中,我们可以通过添加页眉.页脚的方式来丰富文档内容.添加页眉.页脚时,可以添加时间.日期.文档标题,文档引用信息.页码.内容解释.图片/LOGO等多种图文信息.同时也可根据需要调整文字或 ...

  5. C# 给现有PDF文档添加页眉、页脚

    概述 页眉页脚是一篇完整.精致的文档的重要组成部分.在页眉页脚处,可以呈现的内容很多,如公司名称.页码.工作表名.日期.图片,如LOGO.标记等.在之前的文章中介绍了如何通过新建一页空白PDF页来添加 ...

  6. CAD在网页中打印的图纸里面添加页眉及页脚

    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 3 ...

  7. NCreport报表控件教程:设计页眉和页脚

    一.设计页眉 一般来说页眉部分一般是用于包含标题的内容, 首先我们会添加列标签到页眉部分,标签都是简单的文本,标签项一般是用于在报表上显示一些描述信息,标签都是静态项,所以它们的值不会有变化. 添加标 ...

  8. iText5报表_页眉与页脚

    1.概述       iText5中并没有之前版本HeaderFooter对象设置页眉和页脚,可以利用PdfPageEventHelper来完成页眉页脚的设置工作.PdfPageEventHelper ...

  9. word页眉与页脚详解

    1.如何隔离封面等不需要插入页码的页面: 首先插入分节符下一页(一定是分节符),再在下一页(即要开始插入页码的页面)选择视图-->页眉和页脚-->设置为取消链接到前一页.设置页码格式为起始 ...

随机推荐

  1. c++构造函数析构函数调用顺序

    #include <iostream> using namespace std; class A { public: A () { cout<<"A 构造 " ...

  2. java读文件的几个类

    链接地址:http://blog.sina.com.cn/s/blog_407a68fc0100f628.html 最初Java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Wri ...

  3. java生成压缩图

    链接地址:http://blog.sina.com.cn/s/blog_407a68fc0100nrba.html package util; import java.awt.image.Buffer ...

  4. java String 怎么看里面有几个指定字符

    我现在有一个String 字符串,我想看一下这个字符串里有几个指定的字符,比如指定字符是div求解 public class Main { public static void main(String ...

  5. Android 中 ListView Adapter getView 被多次调用问题 解决方法

    执行多次原因是因为每显示一个VIew,它都去测量view的高度,执行measure方法,导致getView执行多次. 解决方法是将 ListView 的 layout_width 设置为 fill_p ...

  6. Codeforces 490B Queue【模拟】

    题意还是很好理解的,根据题目给出描述条件然后求出这串QUEUE 我的做法就是用两个数组 before[] 和 after[] 表示 ai 前面的前面的人的学号 和 ai 后面的后面的人的学号 ex[] ...

  7. java生产者消费者问题代码分析

    作者要的是一个生产者生成,接着必须有一个消费者消费,那这不是需要单线程吗?或者使用1个大小的阻塞队列.所以只谈论问题本身,不谈论好不好. 具体代码: import java.util.concurre ...

  8. 使用Visual Studio将Objective-C编译C++

    编译器支持 谷歌和苹果应用Clang由于他们的C++前端.为了使他们的执行代码Windows上,微软不得不Visual C++C2和Clang结合起来. (Clang是一个C语言.C++.Object ...

  9. Java NIO与IO

    当学习了Java NIO和IO的API后,一个问题立即涌入脑海: 我应该何时使用IO,何时使用NIO呢?在本文中,我会尽量清晰地解析Java NIO和IO的差异.它们的使用场景,以及它们怎样影响您的代 ...

  10. RelativeLayout的属性详解

    1. android:layout_below="@+id/first" //在某元素的的下方: android:layout_alignBottom="@+id/fir ...