Table view 备忘

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
    }
时间: 2024-08-02 11:00:40

Table view 备忘的相关文章

Scroll view 备忘

Stroyboard中使用ScrollView 当我们使用Storyboard开发项目时,如果要往控制器上拖入一个ScrollView并且添加约束设置滚动区域,是有特殊的规定的: 拖入一个scrollview,规定它的位置大小: 如果想要scrollview滚动的话,需要一个view作为scrollview的子视图,称之为 containerView,并且containerView的约束决定着scroll view 是否可以滚动 注意:在实际开发中,我们可能会遇到不止上下滚动(设置水平居中和高度

Colletion View 简单的备忘

UIColletionView 这篇只是做UIColletionView的常用属性.代理方法和数据源方法的备忘,之后做一些自定义布局,增加删除动画等. UIColletionViewFlowLayout的常用属性和UIColletionView的常用属性方法 // 创建布局类 let layout = UICollectionViewFlowLayout() layout.itemSize = CGSizeMake(50.0, 50.0) layout.headerReferenceSize =

Objective-C教程备忘单

终极版本的Objective-C教程备忘单帮助你进行iOS开发. 想开始创建你的第一个iOS应用程序么?那么看一下这篇很棒的教程吧:Create your first iOS 7 Hello World Application 注:这篇文章我写了三天,可能在一些必要的地方使用了编辑和说明,所以如果有任何疑问和修改建议请在下方评论. 这不是一个初学者指南,也不是关于Objective-C的详细讨论,这是关于常见的和高水平的论题的快速索引. 如果这里有些问题没有涉及到,你也可以查阅以下文章: Obj

SQL Server -- 自定义函数(学习总结,备忘)

SQL Server自定义函数,以前只在书上看过,没有动手去敲一敲,今天刚好接触到,看了几篇博文学习了下.做好备忘很重要!! (@[email protected])Y Learn from:http://www.cnblogs.com/lideng/archive/2013/04/15/3022418.html 自定义函数分为:标量值函数或表值函数两种. 标量值函数:如果 RETURNS 子句指定一种标量数据类型,则函数为标量值函数. 表值函数:如果 RETURNS 子句指定 TABLE,则函

AngularJS之备忘与诀窍

译自:<angularjs> 备忘与诀窍 目前为止,之前的章节已经覆盖了Angular所有功能结构中的大多数,包括指令,服务,控制器,资源以及其它内容.但是我们知道有时候仅仅阅读是不够的.有时候,我们并不在乎那些功能机制是如果运行的,我们仅仅想知道如何用AngularJS去做实现一个具体功能. 在这一章中,我么视图给出完整的样例代码,并且对这些样例代码仅仅给出少量的信息和解释,这些代码解决是我们在大多数Web应用中碰到的通用问题.这些代码没有具体的先后次序,你尽可以跳到你关心的小节先睹为快或者

mysql 常用命令(备忘)

1:使用SHOW语句找出在服务器上当前存在什么数据库:mysql> SHOW DATABASES; 2:2.创建一个数据库MYSQLDATAmysql> CREATE DATABASE MYSQLDATA;3:选择你所创建的数据库 mysql> USE MYSQLDATA; (按回车键出现Database changed 时说明操作成功!) 4:查看现在的数据库中存在什么表mysql> SHOW TABLES;5:创建一个数据库表mysql> CREATE TABLE MYT

Express模版引擎hbs备忘

最近几天折腾了下express,想找个合适的模版引擎,下面是一些折腾过程的备忘 选择标准 选择一门模版语言时,可能会考虑的几点 语法友好(micro tmpl那种语法真是够了) 支持模版嵌套(子模版的概念) 支持模版继承(extend) 前后端共用 有容错处理(最好定位到具体出错位置) 支持预编译(性能好) 注意到hbs,似乎满足大部分的需求:https://github.com/donpark/hbs getting started demo地址:https://github.com/chyi

正则表达式入门及备忘

概述 正则表达式,主要是用符号描述了一类特定的文本(模式).而正则表达式引擎则负责在给定的字符串中,查找到这一特定的文本. 本文主要是列出常用的正则表达式符号,加以归类说明.本文仅仅是快速理解了正则表达式相关元字符,作一个备忘,供以后理解更复杂表达式的参考,以后关于正则表达式的相关内容会持续更新本文.示例语言用C# 概述 普通字符 字符集合 速记的字符集合 指定重复次数的字符 匹配位置字符 分支替换字符 匹配特殊字符 组,反向引用,非捕获组 贪婪与非贪婪 回溯与非回溯 正向预搜索.反向预搜索 最

SQL注入备忘单

Find and exploit SQL Injections with free Netsparker SQL Injection Scanner SQL Injection Cheat Sheet, Document Version 1.4 About SQL Injection Cheat Sheet Currently only for MySQL and Microsoft SQL Server, some ORACLE and some PostgreSQL. Most of sam