Todos App

地址
Todos
用Realm数据库
用SWiftUI

Todos的功能:

添加数据
更改数据
删除数据
批量删除数据
移动数据

Main.storyboard:
删除原本的ViewController 创建一个Table View Controller,并且钩上Is Initial View Controller

在这里插入图片描述
在这里插入图片描述
删除左栏的ViewController,在Cocoa Touch Class添加一个TableViewController,命名为TodosController并整理为MVC格式
在这里插入图片描述
Model内创建Todo结构体

import Foundation

struct Todo {
    var name = ""
    var checked = false
}

实例化Todo

class TableViewController: UITableViewController {
    var todos = [
        Todo(name: "吃大象", checked: false),
        Todo(name: "开飞机", checked: false),
        Todo(name: "造坦克", checked: false),
        Todo(name: "坐火箭", checked: false),
        Todo(name: "炸学校", checked: false)
    ]

设置tableview的段数和行数

override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return todos.count
    }

创建TableViewCell
在cell里面增加两个label
在这里插入图片描述
与cell连接

@IBOutlet weak var checkMark: UILabel!
@IBOutlet weak var Todo: UILabel!

更改cell的Accessory属性
将todos里面的Todo依次放入cell中:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "todo", for: indexPath) as! TodoCell
        cell.checkMark.text = todos[indexPath.row].checked ? "√" : ""
        cell.todo.text = todos[indexPath.row].name
        // Configure the cell...

        return cell
    }

效果:
在这里插入图片描述

添加点击后打勾与取消打勾效果

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //更改数据
        todos[indexPath.row].checked = !todos[indexPath.row].checked
        //更新视图
        let cell = tableView.cellForRow(at: indexPath) as! TodoCell
        cell.checkMark.text = todos[indexPath.row].checked ? "√" : ""
        tableView.deselectRow(at: indexPath, animated: true)
        
    }
    

实现添加功能
创建TodoController,添加一个text
在这里插入图片描述

用Navigation Controller连接,并添加一个bar button item,与新的controller连接
在这里插入图片描述

设置新的tableview controller的行数列数

override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #wa
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值