var data: [[String]]!

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

let tableView = UITableView(frame: self.view.bounds, style: .Plain)

tableView.dataSource = self

tableView.delegate = self

self.view.addSubview(tableView)

//数据源

let resourcePath = NSBundle.mainBundle().pathForResource("font", ofType: "plist")

self.data = NSArray(contentsOfFile: resourcePath!)! as! [[String]]

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

return self.data.count

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return self.data[section].count

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let identifier = "cell"

var cell = tableView.dequeueReusableCellWithIdentifier(identifier)

if cell == nil {

cell = UITableViewCell(style: .Default, reuseIdentifier: identifier)

}

cell?.textLabel?.text = self.data[indexPath.section][indexPath.row]

return cell!

}

//section的头视图标题

//    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

//        return "第\(section)个组的头视图"

//    }

//

//    //section的尾视图标题

//    func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {

//        return "第\(section)个组的尾视图"

//    }

//自定义头视图

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

//让headerView作为组的头视图,不需要设置headerView的frame,但是一定要指定高度

let headerView = UIView(frame: CGRectZero)

headerView.backgroundColor = UIColor.grayColor()

let titleLabel = UILabel(frame: CGRect(x: 130, y: 10, width: 150, height: 20))

titleLabel.font = UIFont.boldSystemFontOfSize(16)

titleLabel.textColor = UIColor.greenColor()

titleLabel.text = "第\(section)个组的头视图"

headerView.addSubview(titleLabel)

return headerView

}

//自定义尾部视图

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

let footerView = UIView(frame: CGRectZero)

footerView.backgroundColor = UIColor.darkGrayColor()

let titleLabel = UILabel(frame: CGRect(x: 130, y: 40, width: 150, height: 20))

titleLabel.font = UIFont.boldSystemFontOfSize(16)

titleLabel.textColor = UIColor.redColor()

titleLabel.text = "第\(section)个组的尾视图"

footerView.addSubview(titleLabel)

return footerView

}

//指定组的尾部视图的高度

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

return 100

}

//指定组的头视图的高度

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

return 50

}

Swift自定义头视图和尾视图的更多相关文章

  1. UICollectionview的头视图和尾视图

    UITableView有头视图和尾视图,那么UICollectionView有没有头视图和尾视图呢? 答案是有的. 1.新建一个类,必须继承自 UICollectionReusableView. 2. ...

  2. iOS TabeView 头视图和尾视图不滑动的实现

    因项目有需求不能让section中的头尾视图滑动,顾根据网上的一些资料和自己整理的思路,已实现了不滑动效果,上代码,啥都说了,搞了2个小时都是泪.... 1.创建一个tableview _mainTa ...

  3. iOS之UITableView组头组尾视图/标题悬停

    最近笔者在公司的iOS开发中,有一个iOS开发同事跑来问了两个问题:1.给UITableView设置了组头和组尾视图,但是一直显示不出来?2.UITableView的section的header和fo ...

  4. ##DAY3 自定义视图、视图控制器、视图控制器指定视图、loadView、 viewDidLoad、MVC、屏幕旋转、内存警告

    ##DAY3 自定义视图.视图控制器.视图控制器指定视图.loadView. viewDidLoad.MVC.屏幕旋转.内存警告 #pragma mark ———————自定义视图的步骤 —————— ...

  5. 隐藏自定义的tabbar之后,push到B视图,B视图的键盘工具条无法响应点击事件

    我的情况如下: 在TabbarViewController中隐藏了系统的tabbar,然后自定义tabbar,A B C D 4个视图都有UINavigationController,A视图 使用的是 ...

  6. SpringMVC自定义视图Excel视图和PDF视图

    SpringMVC自定义视图 Excel视图和PDF视图 SpringMVC杂记(十一) 使用Excel视图 Spring MVC 视图解析器(ViewResolver ) java实现导出excel ...

  7. ShareSDK(iOS版)开发实践:自定义授权视图和分享视图导航栏

    最近很多人问ShareSDK的授权视图和分享视图的导航栏样式与应用风格不一致,能否修改导航栏的样式?那么这里我就2.6.1版本进行说明(还在使用1.x版本的朋友建议升级到2.x版本,在新版本中可定制的 ...

  8. IOS中在自定义控件(非视图控制器)的视图跳转中 代理方法与代码块的比较

    //代码块与代替代理的设计方法 我就以在自定义视图中(非视图控制器,不能实现视图控制功能),通过代理和代码块两种方法分别实现视图的跳转,进行对比 首先自定义了一个视图,上面有一个已经注册了得BUtto ...

  9. iOS10 UI教程视图的绘制与视图控制器和视图

    iOS10 UI教程视图的绘制与视图控制器和视图 iOS10 UI视图的绘制 iOS10 UI教程视图的绘制与视图控制器和视图,在iOS中,有很多的绘图应用.这些应用大多是在UIView上进行绘制的. ...

随机推荐

  1. 如何确定Hadoop中map和reduce的个数--map和reduce数量之间的关系是什么?

    一般情况下,在输入源是文件的时候,一个task的map数量由splitSize来决定的,那么splitSize是由以下几个来决定的 goalSize = totalSize / mapred.map. ...

  2. python 2.7中文字符串的匹配(参考)

    #!/bin/env python #-*- coding:utf-8 -*- import urllib import os,sys,json import ssl context = ssl._c ...

  3. cocos代码研究(13)Widget子类EditBox学习笔记

    理论基础 一个用来输入文本的类,继承自 Widget , 以及 IMEDelegate. 代码部分 Public枚举类型 enum KeyboardReturnType键盘的返回键类型. enum I ...

  4. vuex的一个坑

    1  error in callback for watcher "function (){ return this._data.$$state }" 用深拷贝解决 2 接口依赖: ...

  5. Tomcat启动报错:StandardServer.await: create[8005] java.net.BindException: Cannot assign requested address

    Tomcat启动报错:StandardServer.await: create[8005] java.net.BindException: Cannot assign requested addres ...

  6. 20145303 刘俊谦《网络对抗》shellcode注入&Return-to-libc攻击深入

    20145303 刘俊谦<网络对抗>shellcode注入&Return-to-libc攻击深入 Shellcode注入 shellcode实际是一段代码,但却作为数据发送给受攻击 ...

  7. 各版本的区别及含义(i386 、x86_64 、ppc )

    1.i386:是指兼容Intel 80386处理器     x86或80x86是英代爾Intel首先开发制造的一种微处理器体系结构的泛称.該系列較早期的處理器名稱是以數字來表示,並以“86”作為結尾, ...

  8. LA 4253 箭术(二分枚举)

    https://vjudge.net/problem/UVALive-4253 题意: 有n个平行于x轴的线段,每条线段代表一个靶子.判断是否可以站在x轴上[0,W]区间内的某个位置射箭. 思路:二分 ...

  9. Linux中重定向--转载

    转:http://blog.csdn.net/songyang516/article/details/6758256 1重定向 1.1      重定向符号 >               输出 ...

  10. python 读空的json文件

    读空的json文件,python2和python3 的错误提示是不一样的 python2: ValueError: No JSON object could be decoded python3: j ...