美文网首页
《iOS 移动开发》读书笔记_第八章UITableView&UI

《iOS 移动开发》读书笔记_第八章UITableView&UI

作者: 遗忘艳阳天 | 来源:发表于2017-08-22 08:34 被阅读0次

创建表格的代理协议与数据源协议:


        class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {}


代码创建表格对象:


        let tableView = UITableView(frame: CGRect)

        tableView.delegate = self

        tableView.dataSource = self

        self.view.addSubview(tableView)

            //viewDidLoad方法中


获得主屏的尺寸代码:


        let screenRect = UIScreen.main.bounds


表格对象行数的方法:


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

            return Int

        }


表格初始化与Cell复用的方法:


        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            let identifier = ""

            var cell = tableView.dequeueReusableCell(withIdentifier: identifier)

            if(cell == nil){

                cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: identifier)

            }

            return cell!

        }


UITableViewDatasource的主要代理方法_初始化与复用方法tableView(_:cellForRowAt:) //(必须)

UITableViewDatasource的主要代理方法_设置Cell行数的方法: tableView(_:numberOfRowsInSection:) (必须)

UITableViewDatasource的主要代理方法_设置Section的数量: numberOfSections(in:) 

UITableViewDatasource的主要代理方法_设置Section标题文字的方法: tableView(_:titleForHeaderInSecton:) 

UITableViewDatasource的主要代理方法_表格编辑模式方法: tableView(_:canEditRowAt:) 

UITableViewDatasource的主要代理方法_完成插入或删除事件后调用的方法: tableView(_:commit:forRowAt:) 

UITableViewDatasource的主要代理方法_设置Cell可移动的方法: tableView(_:canMoveRowAt:) 

UITableViewDatasource的主要代理方法_Cell移动调用的方法: tableView(_:moveRowAt:to:) 

Cell的预制Style_image-label:UITableViewCellStyle.default 

Cell的预制Style_image-label-label:UITableViewCellStyle.value1

Cell的预制Style_label-label:UITableViewCellStyle.value2

Cell的预制Style_image-label/small_label:UITableViewCellStyle.subtitle 

自定义Cell第一步: 新建一个swift文件(例如tableViewCellDiy.swift)

UITableviewCell.swift所属的类: class CustomizeUITableViewCell: UITableViewCell {}

自定义Cell初始化方法:


        override init(style: UITableViewCellStyle, reuseIdentifier: String?) {}

         required init?(coder aDecoder: NSCoder) {}


创建Cell的image:


        var thumbnail: UIImageView!

        override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

            super.init(style: style, reuseIdentifier: reuseIdentifier)

        }


创建Cell的label:


        var title: UILabel!

        override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

            super.init(style: style, reuseIdentifier: reuseIdentifier)

            self.title = UILabel(frame: CGRect(x: Int, y: Int, width: Int, height: Int))

            self.title.text = ""

            self.addSubview(self.title)

         }


创建Cell的button:


        var detail: UIButton!

        override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

            super.init(style: style, reuseIdentifier: reuseIdentifier)

            self.detail = UIButton(frame: CGRect(x: Int, y: Int, width: Int, height: Int))

            self.detail.setTitle("", for: UIControlState())

            self.addSubview(self.detail)

        }


自定义Cell文件(tableViewCellDiy.swift)与表格文件相关联:


        //在表格视图swift文件中,添加cellForRowAt方法

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            let identifier = "reusedCell"

            var cell: tableViewCellDiy? = tableView.dequeueReusableCell(withIdentifier: identifier)as? tableViewCellDiy

            if cell == nil{

                cell = tableViewCellDiy(style: UITableViewCellStyle.default, reuseIdentifier: identifier)

            }

            return cell!

        }


表格Cell高度的方法(高度为80):


        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

            return 80

        }


UITableViewDelegate主要的代理方法 _设置Cell高度:tableView(_:heightForRowAt:) 

UITableViewDelegate主要的代理方法 _Cell将被显示调用的方法:tableView(_:willDisplay:forRowAt:) 

UITableViewDelegate主要的代理方法 _Cell被点击调用的方法:tableView(_:didSelectRowAt:) 

UITableViewDelegate主要的代理方法 _已被选中的Cell被点击调用的方法:tableView(_:didDeselectRowAt:) 

UITableViewDelegate主要的代理方法 _设置section的头部:tableView(_:viewForHeaderInSection:) 

UITableViewDelegate主要的代理方法 _设置section的尾部:tableView(_:viewForFooterInSection:) 

表格的section和索引:书第198页

响应Cell点击事件的方法: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {}

被点击Cell附件的勾选图标代码:


        let cell = tableView.cellForRow(at: indexPath)

        if cell?.accessoryType == UITableViewCellAccessoryType.none {

            cell?.accessoryType = UITableViewCellAccessoryType.checkmark

        }else{

            cell?.accessoryType = UITableViewCellAccessoryType.none

        }

        //tableView(didSelectRowAt)方法中


Cell附件(UITableViewCellAccessoryType)类型_无: UITableViewCellAccessoryType.none 

Cell附件(UITableViewCellAccessoryType)类型_ⓘ:UITableViewCellAccessoryType.detailButton 

Cell附件(UITableViewCellAccessoryType)类型_ⓘ>:UITableViewCellAccessoryType.detailDisclosureButton 

Cell附件(UITableViewCellAccessoryType)类型_>:UITableViewCellAccessoryType.disclosureIndicator 

Cell附件(UITableViewCellAccessoryType)类型_✔️:UITableViewCellAccessoryType.checkmark 

表格的编辑模式:


        tableView.setEditing(editing: Bool, animated: Bool)

        //editing编辑模式(默认false),animated动画

        //viewDidLoad方法中


Cell编辑属性的方法(属性设置为删除):


        func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {

        return UITableViewCellEditingStyle.delete

        }


3.响应Cell删除事件的方法与代码:


        func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

            if editingStyle == UITableViewCellEditingStyle.delete {

            items.remove(at: indexPath.row)

            let indePaths = [indexPath]

             tableView.deleteRows(at: indePaths, with: UITableViewRowAnimation.automatic)

            }

        }

        //items是数据源变量


数组的长度: Array.count

UITableViewCell编辑类型_默认模式:UITableViewCellEditingStyle.none 

UITableViewCell编辑类型_删除模式:UITableViewCellEditingStyle.delete 

UITableViewCell编辑类型_插入模式:UITableViewCellEditingStyle.insert 

表格插入Cell代码:


        func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {

            return UITableViewCellEditingStyle.insert

        }

        func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

            if editingStyle == UITableViewCellEditingStyle.insert {

            items.insert("", at: indexPath.row)

            let indePaths = [indexPath]

            tableView.insertRows(at: indePaths, with: UITableViewRowAnimation.right)

            }

        }

        //items是数据源变量


表格移动Cell代码:


         func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {

            return true

        }

        func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {

            let fromRow = sourceIndexPath.row

            let toRow = destinationIndexPath.row

            let obj = items[fromRow]

            items.remove(at: fromRow)

            items.insert(obj, at: toRow)

        }

        //items是数据源变量


表格之间的嵌套:书第213页

213

表格Cell图标和高亮图标代码:


        cell?.imageView?.image = UIImage(named: “”)

        cell?.imageView?.highlightedImage = UIImage(named: “”)

        //tableView(cellForRowAt)方法中


Cell标题文字:cell?.textLabel?.text = ""  //tableView(cellForRowAt)方法中

Cell细节标题文字: cell?.detailTextLabel?.text = ""  //tableView(cellForRowAt)方法中

Cell背景颜色为蓝色: cell?.backgroundColor = UIColor.blue  //tableView(cellForRowAt)方法中

滑动到指定Cell:


        let indexPath = IndexPath(row: Int, section: Int)

        tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.top, animated: true)

        //viewDidLoad方法中


获取当前Cell在section的行数:let rowNum = indexPath.row  //tableView(cellForRowAt)方法中

Cell的accessory按钮点击事件调用的方法: func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {}

UICollectionView实例:书第221-225

相关文章

  • 《iOS 移动开发》读书笔记_第八章UITableView&UI

    创建表格的代理协议与数据源协议: class ViewController: UIViewControll...

  • 完美的滑动UITableView

    我已经在这这个最好的移动开发平台上开发很多年了 - iOS,在这区间我已经了解很多iOS apps和很多移动开发人...

  • 常见meta和link标签

    页面基本设置 http 信息设置 搜索引擎相关设置 移动端开发 移动开发基本设置 iOS 图标 iOS启动画面 其...

  • IOS软件开发工程师就业前景好不好?薪资待遇高吗?

    IOS开发的就业前景 近几年随着互联网移动端的快速发展,移动开发已经是必不可少的了。由于国内iOS开发起步相对较晚...

  • KVC

    KVC原理剖析 - CocoaChina_让移动开发更简单 iOS开发底层细究:KVC和KVO底层原理 | iOS...

  • 2016移动开发技术巡礼

    原创 2016-12-27 徐川 移动开发前线 目录 前言 平台篇 iOS平台 Android平台 iOS开发技术...

  • Flutter 探索(一)入门前言

    移动端现状 移动应用已经十年有余,如今,原生开发 Android 可以使用 Java、Kotlin 开发,iOS ...

  • Flutter RN 原生对比

    移动端跨平台开发技术演进 现在主流的移动开发平台是Android和iOS,之前还有过windows phone,每...

  • Masonry

    Masonry介绍与使用实践 - iOS移动开发周报 - 推酷

  • 联机共读11期 - 3.8 - -符楠

    第八章读书笔记 - 数字广告(上):怎样投放数字广告更有效 在移动互联网时代,用户的使用和消费习惯大多转移到了移动...

网友评论

      本文标题:《iOS 移动开发》读书笔记_第八章UITableView&UI

      本文链接:https://www.haomeiwen.com/subject/ofmcdxtx.html