暑期项目开发实训 Day3

来源:互联网 发布:宁夏广播电视网络缴费 编辑:程序博客网 时间:2024/05/22 05:05

        今天开始做第2个Demo。内容是一个Checklists。

        Checklists是App Store中比较热门的一类应用,仅次于健身App(作者如是说,其实我也不信)。

        做Checklists时需要用到TableViewController。主要的设计元素是Table和Navigation。


        注意到,采用tableView需要实现数据源协议:

    // two data source protocol
    // the data source is the link between your data and the table view.

        我们重写两个tableView的函数:

    // find how many rows there are.    override func tableView(_ tableView: UITableView,                            numberOfRowsInSection section: Int) -> Int {        return 100 // there is only m rows, so return m    }        // ask the data source for a cell.    override func tableView(_ tableView:UITableView,                            cellForRowAt indexPath: IndexPath) -> UITableViewCell {                // indexPath is an obj that points to a specific row in the table.        let cell = tableView.dequeueReusableCell(            withIdentifier: "ChecklistItem", for: indexPath)                let label = cell.viewWithTag(1000) as! UILabel                if indexPath.row % 5 == 0 {            label.text = "Walk the dog"        } else if indexPath.row % 5 == 1 {            label.text = "Brush my teeth"        } else if indexPath.row % 5 == 2 {            label.text = "Learn iOS development"        } else if indexPath.row % 5 == 3 {            label.text = "Soccer practice"        } else if indexPath.row % 5 == 4 {            label.text = "Eat ice cream"        }                        return cell  // to obtain the cell for that row    }

        需要注意的是, tableView中 Cell 和 Row的关系。

        简单而言,cell是页面中出现的容器, Row是容器中的包含数据的具体内容的。

        // ways to create cells in tableView
        // 1. add a prototype cell to the table view in the storyboard
        // 2. set a reuse identifier on the prototype cell;
        // 3. call tableView.dequeReusableCell(withIdentifier);
    
        // You will usually have fewer cells than rows. For run out of memory.

         所以我们会发现Cell一般是少于Row的,因为防止内存耗尽。


        最后我尝试了tap后让row变成de-selected,以及toggle the checkmarks。

    // tap后消失 变成 de-selected    勾选功能  toggle the checkmarks.    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {                if let cell = tableView.cellForRow(at: indexPath) {            if cell.accessoryType == .none {                cell.accessoryType = .checkmark            } else {                cell.accessoryType = .none            }        }        tableView.deselectRow(at: indexPath, animated: true)    }

        今天明天早上要考毛概了,所以今天实在无心恋战~ 只完成了一点点工作。

原创粉丝点击