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. get改造成post请求

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  2. [C#] 常用工具类——文件操作类

    /// <para> FilesUpload:工具方法:ASP.NET上传文件的方法</para> /// <para> FileExists:返回文件是否存在&l ...

  3. JAVA IO之管道流总结大全(转)

    要在文本框中显示控制台输出,我们必须用某种方法“截取”控制台流.换句话说,我们要有一种高效地读取写入到System.out和 System.err 所有内容的方法.如果你熟悉Java的管道流Piped ...

  4. JSP/Servlet 中的事件处理

    写过AWT或Swing程序的人一定对桌面程序的事件处理机制印象深刻:通过实现Listener接口的类可以在特定事件(Event)发生时,呼叫特定的方法来对事件进行响应. 其实我们在编写JSP/Serv ...

  5. Java基础知识强化之网络编程笔记02:Socket通信原理图解

    1. Socket (1)Socket套接字  网络上具有唯一标识的IP地址和端口号组合在一起才能构成唯一能识别的标识符套接字 (2)Socket原理机制:  • 通信两端都有Socket.  • 网 ...

  6. 解决iScroll中事件点击一次却触发两次的问题

    var t1=null;//全局 function myClick() { if (t1 == null){ t1 = new Date().getTime(); }else{ var t2 = ne ...

  7. CI框架篇之基础篇(1)

    CodeIgniter 是一套给 PHP 网站开发者使用的应用程序开发框架和工具包.它提供一套丰富的标准库以及简单的接口和逻辑结构, 其目的是使开发人员更快速地进行项目开发.使用 CodeIgnite ...

  8. ASP.Net免费发送短信-阿里大鱼短信接口

    有点短信余额 不用白不用 3月1号就过期了 情人节做了个免费发短信的 http://love.issuc.com/ 固定短信模板 [活动验证]您正在参加XXX的OOO活动,请确认系本人申请.需要的可以 ...

  9. P2P之UDP穿透NAT原理

    首先先介绍一些基本概念:             NAT(Network   Address   Translators),网络地址转换:网络地址转换是在IP地址日益缺乏的情况下产生的,它的主要目的就 ...

  10. MongoDB的Document操作

    简介 一.Document数据插入 二.Document数据删除 三.Document数据更新 一.Document数据插入 1.插入文档 db.[文档名].insert({BSON数据}) 2.批量 ...