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

这篇具有很好参考价值的文章主要介绍了iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.创建新项目
iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1),ios,swift,ui

为项目添加图标
iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1),ios,swift,ui

2.将Table View Controller添加到界面中
iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1),ios,swift,ui

将箭头移动到Table View上来,代表它是首页(根页面).选中ViewController,点击Delete,对它进行删除.将代码ViewController.swift也删除掉.
iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1),ios,swift,ui

新建一个Cocoa Touch Class.
iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1),ios,swift,uiiOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1),ios,swift,ui

将TableViewController的class设置成TodosViewController.
iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1),ios,swift,ui

2.为cell取名为TodoCellID.
iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1),ios,swift,ui

3.创建一个Button,将Button的Image改为circle.创建一个Lable,将Lable的Lines改为0,可以自动换行.将Button和Lable放到同一个StackView里,设置约束为垂直居中.
iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1),ios,swift,ui

为Button设定宽高约束,为Stack View设定上下左右约束,设定Stack View的Allgnment为Center(所有字样居中),Distribution为Fill,Spacing(Button与Lable的间距)为12.
iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1),ios,swift,ui

4.创建一个UITableViewCell类型的swift,用于动态响应Button勾选以及文本的变化.
iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1),ios,swift,ui

选择TodoCellID的Class为TodoCell.
iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1),ios,swift,ui

5.创建一个Swift文件Todo.把他放到Modle文件夹下.
iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1),ios,swift,ui

设定默认待办事项,并编码,使其展示在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
//    }
//}

启动测试:
iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1),ios,swift,ui

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

将Button的Tint改为Clear Color.使选中后的淡蓝色消失.
iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1),ios,swift,uiiOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1),ios,swift,ui

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.
    }
    

}

启动测试:

iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1),ios,swift,ui文章来源地址https://www.toymoban.com/news/detail-707471.html

到了这里,关于iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • iOS开发Swift-函数

     (1)无参函数 (2)多参函数 (3)无返回值 (4)多重返回值 (5)可选元组返回类型(元组可以是nil) (6)隐式返回的函数 任一可以被写成一行return的函数,return(x) + for。 调用的时候: 方法名(for: 参数) (1)指定参数标签 (2)忽略参数标签 (3)默认参数值 (4)可变参数 一个可变参数可接受0个或多

    2024年02月11日
    浏览(10)
  • iOS开发Swift-集合类型

    集合基本类型:数组 Array (有序), 集合 Set (无序不重复), 字典 Dictionary (无序键值对) (1)数组的表示 (2)创建空数组 (3)带值数组 (4)两数组相加创建数组 (5)字面量创造数组 (6)访问数组 (7)添加 (8)修改 (9)删除 (10)遍历 同时需要索引和值时: (1)集合的表示 (2)构造一个集合 (3)字面

    2024年02月11日
    浏览(13)
  • iOS开发系列--Swift语言

    Swift是苹果2014年推出的全新的编程语言,它继承了C语言、ObjC的特性,且克服了C语言的兼容性问题。Swift发展过程中不仅保留了ObjC很多语法特性,它也借鉴了多种现代化语言的特点,在其中你可以看到C#、Java、Javascript、Python等多种语言的影子。同时在2015年的WWDC上苹果还宣布

    2024年02月06日
    浏览(16)
  • iOS开发Swift-控制流

    (1)复合匹配 (2)区间匹配 (3)元组匹配 (4)值绑定匹配 (5)where continue, break, fallthrough, return, throw continue: 停止本次循环,开始下次循环 break: 立即结束整个控制流。可以使用break忽略switch的分支。 fallthrough贯穿: switch中的case加入贯穿,case会穿透到下一个case/ default。

    2024年02月11日
    浏览(15)
  • iOS开发Swift-类型转换

    1.Int或Double转字符串 2.Double转Int(去掉小数点后面的) 3.Int转Double 4.向上转型 5.向下转型

    2024年02月09日
    浏览(10)
  • iOS开发Swift-基础部分

    系统可通过赋初始值进行自动推断。 平时可加可不加,同一行中有两句话必须加。 Int           UInt(最好不用) Double 64位 很大/高精度情况下使用 15位小数 Float 32位 对精度要求不高的情况下用 6位小数 十进制数   17 二进制 0b前缀 0b10001 八进制 0o前缀 0o21 十六进制 0x前缀

    2024年02月11日
    浏览(11)
  • iOS开发Swift-基本运算符

    一元 单一操作对象 -a    !b    c! 二元 两个操作对象 2 + 3 三元 三目运算符 a ? b : c 赋值运算符不返回任何值,所以 if x = y { ... } 无效。 +   -   *   / 默认不允许数值运算中溢出。 溢出运算符:   a + b a % b = 余数 a = (b * 倍数) + 余数 所以a % b = a % -b ==     !=         

    2024年02月11日
    浏览(11)
  • iOS(一):Swift纯代码模式iOS开发入门教程

    iOS(一):Swift纯代码模式iOS开发入门教程

    1.修改 AppDelegate.swift 和 ViewController.swift 文件 2.删除 SceneDelegate.swift 和 Main.storyboard 文件 3.修改如图所示项 安装 CocoaPods 初始化项目(添加 Podfile 配置文件) 修改 Podfile 文件 安装 打开 ExDemoApp.xcworkspace 项目并向 ViewController.swift 添加示例代码 运行效果 安装 QMUIKit 方式一:触发

    2024年02月09日
    浏览(14)
  • iOS开发Swift-1-Xcode创建项目

    iOS开发Swift-1-Xcode创建项目

    1.创建项目 双击Xcode App,选择Create a new Xcode project。  选择创建一个iOS普通的App项目。选择Single View App,点击Next。  填写项目名,组织名称等,点击next。  选择好文件的存储路径,点击create。  2.为前端添加组件 点击Main,选中View,在右下角show the Object library中找到label组件,

    2024年02月10日
    浏览(13)
  • iOS开发Swift-字符串与字符

     前一个\\\"\\\"\\\"前和后一个\\\"\\\"\\\"后无换行  想要实现在代码编写时换行而在实际运行后不换行:  (1)转义字符 \\0 空字符 \\\\ 反斜线 t 水平制表符 n 换行符 r 回车符 \\\" 双引号 \\\' 单引号 要在\\\"\\\"\\\"中使用(\\\"\\\"\\\")时,必须至少写一个转义符。例如 \\\"\\\"\\\" 或 \\\"\\\"\\\" (2)Unicode标量 u{24} 两位十六进制

    2024年02月11日
    浏览(14)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包