暑期项目开发实训 Day3

        今天开始做第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)
    }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

国产酱香科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值