首页 > 其他分享 >iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1)

iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1)

时间:2023-09-07 18:26:56浏览次数:41  
标签:TableViewController indexPath 12 checked tableView 待办 func override false

1.创建新项目

 为项目添加图标

 2.将Table View Controller添加到界面中

 将箭头移动到Table View上来,代表它是首页(根页面).选中ViewController,点击Delete,对它进行删除.将代码ViewController.swift也删除掉.

 新建一个Cocoa Touch Class.

 

 将TableViewController的class设置成TodosViewController.

 2.为cell取名为TodoCellID.

 3.创建一个Button,将Button的Image改为circle.创建一个Lable,将Lable的Lines改为0,可以自动换行.将Button和Lable放到同一个StackView里,设置约束为垂直居中.

 为Button设定宽高约束,为Stack View设定上下左右约束,设定Stack View的Allgnment为Center(所有字样居中),Distribution为Fill,Spacing(Button与Lable的间距)为12.

 4.创建一个UITableViewCell类型的swift,用于动态响应Button勾选以及文本的变化.

 选择TodoCellID的Class为TodoCell.

 5.创建一个Swift文件Todo.把他放到Modle文件夹下.

 设定默认待办事项,并编码,使其展示在app首页上.

TodosViewController:

import UIKit

class TodosViewController: UITableViewController {
    
    let todos = [
        Todo(name: "学习iOS课程的基础课", checked: false),
        Todo(name: "学习iOS课程的零基础赏月App开发", checked: false),
        Todo(name: "学习iOS课程的零基础木琴App开发", checked: false),
        Todo(name: "学习iOS课程的零基础和风天气App开发", checked: false),
        Todo(name: "学习iOS课程的零基础待办事项App开发", checked: false),
        Todo(name: "学习iOS课程的小红书App开发", checked: false)
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

    // MARK: - Table view data source
    //配置TableView的一些数据

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

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return todos.count   //总共有10个待办事项
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {  //此函数会根据上面两个函数(总共分类数和总共待办事项数)返回的内容多次执行
        let cell = tableView.dequeueReusableCell(withIdentifier: kTodoCellID , for: indexPath) as! TodoCell

//        // Configure the cell...
//        //配置主标题的文本
//        var contentConfiguration = cell.defaultContentConfiguration()
//        contentConfiguration.text = "昵称"   //主标题
//        contentConfiguration.secondaryText = "个性签名"    //副标题
//        contentConfiguration.image = UIImage(systemName: "star")  //图片
//        cell.contentConfiguration = contentConfiguration

        cell.todoLable.text = todos[indexPath.row].name
        return cell
    }
     
    

    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */
    //事件函数
    //当对每一行进行排序时需要调用的方法
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }


    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    

}

TodoCell:

import UIKit

class TodoCell: UITableViewCell {
    @IBOutlet weak var checkBoxBtn: UIButton!
    @IBOutlet weak var todoLable: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

Todo:

import Foundation

struct Todo{   //结构体.struck:值类型,class:引用类型. strack不需要再额外写构造器,因为系统已经自动生成.
    var name: String   //文本
    var checked: Bool   //是否已经完成
}

//相当于class的:
//class Todo{
//    var name = ""
//    var checked = false
//    init(name: String, checked: Bool){
//        self.name = name
//        self.checked = checked
//    }
//}

启动测试:

 6.实现checkBox这个Button被选中之后变色.

将Button的Tint改为Clear Color.使选中后的淡蓝色消失.

 TodosViewController:

import UIKit

class TodosViewController: UITableViewController {
    
    let todos = [
        Todo(name: "学习iOS课程的基础课", checked: false),
        Todo(name: "学习iOS课程的零基础赏月App开发", checked: false),
        Todo(name: "学习iOS课程的零基础木琴App开发", checked: false),
        Todo(name: "学习iOS课程的零基础和风天气App开发", checked: false),
        Todo(name: "学习iOS课程的零基础待办事项App开发", checked: false),
        Todo(name: "学习iOS课程的小红书App开发", checked: false)
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

    // MARK: - Table view data source
    //配置TableView的一些数据

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

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return todos.count   //总共有10个待办事项
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {  //此函数会根据上面两个函数(总共分类数和总共待办事项数)返回的内容多次执行
        let cell = tableView.dequeueReusableCell(withIdentifier: kTodoCellID , for: indexPath) as! TodoCell

//        // Configure the cell...
//        //配置主标题的文本
//        var contentConfiguration = cell.defaultContentConfiguration()
//        contentConfiguration.text = "昵称"   //主标题
//        contentConfiguration.secondaryText = "个性签名"    //副标题
//        contentConfiguration.image = UIImage(systemName: "star")  //图片
//        cell.contentConfiguration = contentConfiguration

        
        cell.checkBoxBtn.isSelected = todos[indexPath.row].checked  //将cell的是否被选中属性改为todos的当前行的checked属性
        cell.todoLable.text = todos[indexPath.row].name
        return cell
    }
     
    

    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */
    //事件函数
    //当对每一行进行排序时需要调用的方法
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }


    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    

}

TodoCell:

import UIKit

class TodoCell: UITableViewCell {
    @IBOutlet weak var checkBoxBtn: UIButton!
    @IBOutlet weak var todoLable: UILabel!
    
    override func awakeFromNib() {   //每个cell加载出来之后执行的函数
        //dequeueReusableCell -> awakeFromNib -> dequeueReusableCell后面的内容
        super.awakeFromNib()
        // Initialization code
        checkBoxBtn.setImage(UIImage(systemName: "checkmark.circle.fill"), for: .selected)
        
    }//设定了当前button被选中之后里边的图片
    

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

7.实现checkBox这个Lable被选中之后字体变灰色.

 TodosViewController:

import UIKit

class TodosViewController: UITableViewController {
    
    let todos = [
        Todo(name: "学习iOS课程的基础课", checked: false),
        Todo(name: "学习iOS课程的零基础赏月App开发", checked: true),
        Todo(name: "学习iOS课程的零基础木琴App开发", checked: false),
        Todo(name: "学习iOS课程的零基础和风天气App开发", checked: false),
        Todo(name: "学习iOS课程的零基础待办事项App开发", checked: false),
        Todo(name: "学习iOS课程的小红书App开发", checked: false)
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

    // MARK: - Table view data source
    //配置TableView的一些数据

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

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return todos.count   //总共有10个待办事项
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {  //此函数会根据上面两个函数(总共分类数和总共待办事项数)返回的内容多次执行
        let cell = tableView.dequeueReusableCell(withIdentifier: kTodoCellID , for: indexPath) as! TodoCell

//        // Configure the cell...
//        //配置主标题的文本
//        var contentConfiguration = cell.defaultContentConfiguration()
//        contentConfiguration.text = "昵称"   //主标题
//        contentConfiguration.secondaryText = "个性签名"    //副标题
//        contentConfiguration.image = UIImage(systemName: "star")  //图片
//        cell.contentConfiguration = contentConfiguration

        
        cell.checkBoxBtn.isSelected = todos[indexPath.row].checked  //将cell的是否被选中属性改为todos的当前行的checked属性
        cell.todoLable.text = todos[indexPath.row].name
        cell.todoLable.textColor = todos[indexPath.row].checked ? .tertiaryLabel : .label   //三元运算符.根据是否被选中进行判断,如果被选中的话变成浅色,未被选中就是原来的Lable Color.
        return cell
    }
     
    

    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */
    //事件函数
    //当对每一行进行排序时需要调用的方法
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }


    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    

}

启动测试:

 


 

标签:TableViewController,indexPath,12,checked,tableView,待办,func,override,false
From: https://www.cnblogs.com/lysboke/p/17683050.html

相关文章

  • 12种微服务模式
    释放微服务的力量您是否正在努力构建高效、可扩展且有弹性的软件系统?作为软件开发人员或高级开发人员,您一定遇到过“微服务架构”一词。这种革命性的软件开发方法已被许多成功的科技巨头采用,例如Netflix、亚马逊和Spotify。但是,微服务到底是什么,你为什么要关心?微服务架构是一种软......
  • 123
    W0G2X0W2IM-eyJsaWNlbnNlSWQiOiJXMEcyWDBXMklNIiwibGljZW5zZWVOYW1lIjoiVU5JVkVSU0lUQVMgSVNMQU0gTkVHRVJJIFNVTkFOIEtBTElKQUdBIiwiYXNzaWduZWVOYW1lIjoiY29kZSBtYW5vbmciLCJhc3NpZ25lZUVtYWlsIjoiMjQxNjc0NjA1QHFxLmNvbSIsImxpY2Vuc2VSZXN0cmljdGlvbiI6IkZvciBlZHVjYXRpb25h......
  • Adobe Lightroom Classic 12.4详细安装教程
    AdobeLightroomClassic12.4的新增功能包括:借助AI支援的降噪功能,可以轻松、有效的消除RAW图像中的杂色,同时保留精细细节。使用新蒙版功能对图像进行清晰的编辑。体验人物蒙版中的新增功能,可以自动选择脸部毛发。通过对照片进行终极图像控制以获得最佳效果。修复面板中新增的内......
  • 力扣---1123. 最深叶节点的最近公共祖先
    给你一个有根节点 root 的二叉树,返回它 最深的叶节点的最近公共祖先 。回想一下:叶节点 是二叉树中没有子节点的节点树的根节点的 深度 为 0,如果某一节点的深度为 d,那它的子节点的深度就是 d+1如果我们假定 A 是一组节点 S 的 最近公共祖先,S 中的每个节点都在......
  • 12.NAT
    NATNAT(NetworkAddressTranslation网络地址转换技术),作用是将内网私有地址转换成公网地址,使得内网的主机可以上外网。私有地址:任何人都可以使用10.0.0.0/8172.16.0.0-172.31.255.255192.168.0.0/16基础配置:配置好ip地址出口缺省路由静态NATstaticNAT:一对一(一一映射......
  • 【叮当待办】内购项目
    【叮当待办】永久会员说明- 周期:永久有效  ,费用¥48【叮当待办自动续费VIP订阅说明】-订阅周期与费用:周期:1个月(连续包月产品)费用:¥3周期:1年(连续包年产品)  费用:¥28-付费:您的iTunes账户会在购买确认后¥立即扣款-续订:您的iTunes账户会在到期前24小时内扣......
  • 12 休眠线程
    packageThreadDemo;//1.模拟网络延迟:放大问题发生的情况//2.模拟倒计时publicclassTest12_Sleep{publicstaticvoidmain(String[]args)throwsInterruptedException{tenDown();}publicstaticvoidtenDown()throwsInterruptedExcep......
  • C语言数组(12)——写一个三子棋游戏(3)
    一.回顾我们上篇文章主要介绍了棋盘的打印,我们用到了DisplayBoard()函数,那么我们现在就需要来实现玩家下棋这一操作二.玩家下棋功能的实现与前几个函数一样我们将玩家下棋功能代码封装成一个函数,命名为PlayerMove()函数,我们前面说过玩家下棋的本质就是将数据填进二维数组中的元素中......
  • 128. 最长连续序列
    给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。请你设计并实现时间复杂度为 O(n) 的算法解决此问题。示例1:输入:nums=[100,4,200,1,3,2]输出:4解释:最长数字连续序列是[1,2,3,4]。它的长度为4。示例2:输入:nums=[0,......
  • 用友8V12.0导入凭证时提示外部表不是预期的格式
    用友8V12导入凭证时提示外部表不是预期的格式1.之前可以导入.xlsx,现在只能导入.xls格式2..xls格式下拉公式非同表提取数据,关闭表再打开就不能提取,必需重新再设置。知道解决办法的请留言指导,谢谢!......