Table view 备忘

本篇会以备忘为主,主要是一些基础的代理方法和数据源方法具体的优化好点子会后续跟上。

Table view的数据源方法

必须实现的数据源方法

    // 返回每一行的cell,可以做缓存处理,同样也可能会造成复用问题。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// tableview 和 cell 都是在storyboard拖上去的
let cell = tableview.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)
cell.textLabel?.text = datas[indexPath.row].0
cell.detailTextLabel?.text = datas[indexPath.row].1 return cell
} // 告诉tableview应该有多少行
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datas.count
}

组的方法

    // 告诉tableview一共有多少组,默认是1
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
} // 告诉tableview组尾应该显示的文字,就算没有设置组尾的高度也适用。
func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return "这是组尾"
} // 告诉tableview组头应该显示的文字,就算没有设置组头的高度也适用。
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "这是组头"
}

row的编辑方法

删除:

    // 某一行是否是可以编辑的,但是只设置这个方法是无效的
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
} // 在可以编辑的情况下,通过判断编辑类型,实现具体逻辑;达成真正的可编辑。
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
datas.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
}

添加:

    // 这是代理方法,返回编辑类型
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return .Insert
} // 某一行是否是可以编辑的,但是只设置这个方法是无效的
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
} // 在可以编辑的情况下,通过判断编辑类型,实现具体逻辑;达成真正的可编辑。
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Insert {
tableView.beginUpdates()
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
datas.insert(("New Title", "New Sub Title"), atIndex: indexPath.row)
print(datas)
tableView.endUpdates()
}
}

移动:

    // 首先 进入编辑状态
@IBAction func edit(sender: UIBarButtonItem) {
tableview.setEditing(!tableview.editing, animated: true)
} // 打开编辑模式
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
} // 是否可以移动某一行。
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
} // 移动某一行的具体操作
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
let fromIndex = sourceIndexPath.row
let toIndex = destinationIndexPath.row let moveData = datas[fromIndex]
datas.removeAtIndex(fromIndex)
datas.insert(moveData, atIndex: toIndex)
}

设置索引:

    // 设置边栏的文字(索引),即通讯录的快捷查找 "abc...xyz"
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return ["a", "b"]
} // 索引点击事件
func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
// 点击索引,跳到对应的列组就ok
tableView .scrollToRowAtIndexPath(<#T##indexPath: NSIndexPath##NSIndexPath#>, atScrollPosition: <#T##UITableViewScrollPosition#>, animated: <#T##Bool#>)
}

Table view的代理方法

cell header footer的显示与停止显示:

    // 即将展示cell
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
} // 即将显示footerview
func tableView(tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) { } // 即将显示headerview
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { } // 当某一行footerView停止显示的时候,就是footerView从屏幕上消失的时候 调用这个方法
func tableView(tableView: UITableView, didEndDisplayingFooterView view: UIView, forSection section: Int) { } // 当某一行headerView停止显示的时候,就是headerView从屏幕上消失的时候 调用这个方法
func tableView(tableView: UITableView, didEndDisplayingHeaderView view: UIView, forSection section: Int) { } // 当某一行cell停止显示的时候,就是cell从屏幕上消失的时候 调用这个方法
func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { }

cell header footer的高度

    // 告诉tableview的组头高度
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50.0
} // 告诉tableview的组尾高度
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 50.0
} // 返回每一行cell的高度
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 60.0
} override func viewDidLoad() {
super.viewDidLoad() // 高度也可以通过属性来赋值
self.tableview.rowHeight = 60
self.tableview.sectionHeaderHeight = 60
self.tableview.sectionFooterHeight = 60
}

预估cell header footer的高度。

注意:这个方法需要跟别的方法配合,这个以后会写自适应高度来进行备忘。

    // cell的估计高度
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100.0
} // footer的估计高度
func tableView(tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
return 100.0
} // header的估计高度
func tableView(tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
return 100.0
} override func viewDidLoad() {
super.viewDidLoad()
// 同样也可以通过属性赋值
self.tableview.estimatedRowHeight = 60
self.tableview.estimatedSectionFooterHeight = 60
self.tableview.estimatedSectionFooterHeight = 60
}

自定义组头组尾

    // 返回自定义组尾视图
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footer = UIView()
footer.backgroundColor = UIColor.redColor()
return footer
} // 返回自定义组头视图
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = UIView()
header.backgroundColor = UIColor.yellowColor()
return header
}

accessory点击事件

        // accessory type 为以下两个类型才可以
cell.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton
cell.accessoryType = UITableViewCellAccessoryType.DetailButton // 当点击了附属按钮时触发
func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
print("accessoryButtonTappedForRowWithIndexPath")
}

cell点击后的回调

    // 当cell被点击了,是否显示高亮
func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
} // cell已经进入高亮状态
func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {
// print("didHighlightRowAtIndexPath\(indexPath.row)")
} // 当cell进入高亮状态后调用
func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) {
// print("didUnhighlightRowAtIndexPath\(indexPath.row)")
} // 某一行cell被选中的时候调用,返回哪一行应该被点击
func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
print("willSelectRowAtIndexPath")
return indexPath
} // 某一行cell即将失去被选中状态时调用
func tableView(tableView: UITableView, willDeselectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
print("willDeselectRowAtIndexPath \(indexPath.row)")
return indexPath
} // 当某一行cell已经被选中时调用
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("didSelectRowAtIndexPath")
} // 某一行cell已经失去被选中状态时调用
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
print("didDeselectRowAtIndexPath \(indexPath.row)")
}

编辑状态的样式和自定义

    // 当tableview进入编辑状态时,返回每一行cell的编辑模式 (添加/删除)
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.Delete
} // 当cell向左滑的时候,显示删除按钮的标题
func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
return "删除"
} // iOS8可以用的方法,返回一个UITableViewRowAction数组类型,UITableViewRowAction是可以自定义的,就是自定义左划后出现的编辑按钮
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let action = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "自定义的删除") { (UITableViewRowAction, NSIndexPath) in
print("自定义删除回调")
} let anotherAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "自定义的") { (anotherAction, indexPath) in
print("自定义的回调")
}
anotherAction.backgroundColor = UIColor.blueColor()
return [action, anotherAction]
} func tableView(tableView: UITableView, shouldIndentWhileEditingRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
} // 某一行cell即将进入编辑模式,就是向左滑,编辑按钮显示出来
func tableView(tableView: UITableView, willBeginEditingRowAtIndexPath indexPath: NSIndexPath) {
print("willBeginEditingRowAtIndexPath")
} // 某一行cell已经结束了编辑状态,就是把cell往右滑,关闭编辑按钮
func tableView(tableView: UITableView, didEndEditingRowAtIndexPath indexPath: NSIndexPath) {
print("didEndEditingRowAtIndexPath")
} // 移动行的过程中会多次调用此方法,返回值代表进行移动操作后回到的行
func tableView(tableView: UITableView, targetIndexPathForMoveFromRowAtIndexPath sourceIndexPath: NSIndexPath, toProposedIndexPath proposedDestinationIndexPath: NSIndexPath) -> NSIndexPath {
return proposedDestinationIndexPath
}

cell长按弹出Menu

    // 是否允许长按弹出菜单
func tableView(tableView: UITableView, shouldShowMenuForRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
} // 每行cell需要用的方法。
func tableView(tableView: UITableView, canPerformAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
if action == #selector(NSObject.cut(_:)) {
// 不显示cut功能
return false
}
return true
} // 具体功能的实现
func tableView(tableView: UITableView, performAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) { }

cell的缩进

   // 设置cell的缩进,每一行cell显示的时候调用一次
func tableView(tableView: UITableView, indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int {
print("indentationLevelForRowAtIndexPath")
return 10
}

Table view 备忘的更多相关文章

  1. Scroll view 备忘

    Stroyboard中使用ScrollView 当我们使用Storyboard开发项目时,如果要往控制器上拖入一个ScrollView并且添加约束设置滚动区域,是有特殊的规定的: 拖入一个scroll ...

  2. Colletion View 简单的备忘

    UIColletionView 这篇只是做UIColletionView的常用属性.代理方法和数据源方法的备忘,之后做一些自定义布局,增加删除动画等. UIColletionViewFlowLayou ...

  3. AngularJS之备忘与诀窍

    译自:<angularjs> 备忘与诀窍 目前为止,之前的章节已经覆盖了Angular所有功能结构中的大多数,包括指令,服务,控制器,资源以及其它内容.但是我们知道有时候仅仅阅读是不够的. ...

  4. Objective-C教程备忘单

    终极版本的Objective-C教程备忘单帮助你进行iOS开发. 想开始创建你的第一个iOS应用程序么?那么看一下这篇很棒的教程吧:Create your first iOS 7 Hello Worl ...

  5. PostgreSQL 速查、备忘手册 | PostgreSQL Quick Find and Tutorial

    PostgreSQL 速查.备忘手册 作者:汪嘉霖 这是一个你可能需要的一个备忘手册,此手册方便你快速查询到你需要的常见功能.有时也有一些曾经被使用过的高级功能.如无特殊说明,此手册仅适用于 Linu ...

  6. [原]TCP/UDP使用细节备忘

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  7. 在iOS中怎样创建可展开的Table View?(上)

    原文地址 本文作者:gabriel theodoropoulos 原文:How To Create an Expandable Table View in iOS 原文链接 几乎所有的app都有一个共 ...

  8. HTML5终极备忘大全

    二.文字备忘之标签 HTML5中新增的标签 <article> 定义文章 <aside> 定义页面内容旁边的内容 <audio> 定义声音内容 <canvas ...

  9. [转] HTML5终极备忘大全(图片版+文字版)---张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=1544 一.前言兼图片 ...

随机推荐

  1. Android-Unable to resolve target 'android-8'

  2. [每日一题] 11gOCP 1z0-052 :2013-08-31 数据库的存储结构....................................................A8

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/10784599 . 正确答案:A 将逻辑存储与物理存储分开是关系数据库范例的必要部分.关系数 ...

  3. unix进程的环境--unix环境高级编程读书笔记

    http://blog.csdn.net/xiaocainiaoshangxiao/article/category/1800937

  4. Linux下Wireshark普通用户不能获取网络接口问题

    Linux下Wireshark普通用户不能获取网络接口问题 1.安装setcap, setcap 是libcap2-bin包的一部分,一般来说,这个包默认会已经装好. sudo apt-get ins ...

  5. GUI编程笔记(java)04:GUI(HelloWorld)窗体案例

    1.Frame 在JAVA中,Frame是一种控件,可作为父窗体加载其他swing控件.案例: package cn.itcast_01; import java.awt.Frame; public ...

  6. 在iis中mantisbt配置过程

    最近需要安装个mantisbt,由于不想再安装个apache服务器,因此直接使用iis作为php解析服务器.同时为了方便管理安装包,将php安装包和扩展包能够独立存放在D:\Program Files ...

  7. 请问JAVA三层架构,持久层,业务层,表现层,都该怎么理解?和MVC三层模型有什么区别

    持久层用来固化数据,如常说的DAO层,操作数据库将数据入库业务层用来实现整体的业务逻辑 如 前台获得了数据,逻辑层去解析这些数据,效验这些数据等操作表现层很好解释  你现在看到的网页 一些界面 都属于 ...

  8. 网页版 treeview使用中遇到的问题

    <div class="ScrollBar" id="ItemsTree"></div> var cla = $("#Item ...

  9. SqlSugar框架T4模板的使用

    一.T4模板说明 1.T4模板是用来生成Model层实体类的 2.文件后缀为.tt 3.需要修改配置主要有:引用的SqlSugar.dll的位置.生成实体类的位置及生成实体类的命名空间 4.T4模板生 ...

  10. C# 读取txt文本内容写入到excel

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...