zoukankan      html  css  js  c++  java
  • 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
        }
    
  • 相关阅读:
    bootstrap的验证和确认对话框
    Jquery.Datatables 基本创建方法
    DOM – (w3school)1.DOM 方法 + 2.DOM属性 + 3.DOM 元素
    Jquery.Datatables 基本设置的中文注解
    修改android studio中的avd sdk路径、avd sdk找不到的解决方案
    一个人至少是需要学习3种语言的
    Framework7:不会Objective-C,也能开发iOS7应用
    私人定制,十款最佳Node.js MVC框架
    PHP开发框架流行度排名:Laravel居首
    仅用移动开发服务:开发native应用
  • 原文地址:https://www.cnblogs.com/Alex-sk/p/5551013.html
Copyright © 2011-2022 走看看