Swift学习笔记笔记(八) 日期选择和表现视图组件的使用
创始人
2024-03-29 13:32:13
0

一、实验目的:

1.掌握DatePicker组件的使用
2.掌握TableView组件的使用
3.掌握代码设置属性的方法

二、实验原理:

1.属性面板设置属性的缺点
2.DatePicker中Moder属性的设置方法
3.DatePicker中Locale属性的设置方法
4.随机数函数的原型
5. 运动检测函数的原型
6.Xcode导入素材的方法

三、实验步骤及内容:

1.代码如下:
// ViewController.swift
// 日期和时间选择器
// Created by luodev on 2022/8/11.
// Copyright 2022 luodev. All rights reserved.
import UIKit
class ViewController: UIViewController {
var dateFormatter = DateFormatter()
@IBOutlet weak var datePicker: UIDatePicker!
@IBOutlet weak var labelMsg: UILabel!
@IBAction func dateChange(_ sender: UIDatePicker) {
@IBOutlet weak var labelMsg: UILabel!
@IBAction func dateChange(_ sender: UIDatePicker) {
labelMsg.text = dateFormatter.string(from:datePicker.date)
labelMsg.sizeToFit()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
datePicker.datePickerMode=UIDatePicker.Mode.dateAndTime
datePicker.locale=NSLocale(localeIdentifier:“zh_CN”) as Locale
datePicker.date=NSDate() as Date
dateFormatter.dateFormat=“公元y年M月d日hh点mm分ss秒”
labelMsg.text=dateFormatter.string(from:datePicker.date)
labelMsg.sizeToFit()
}
}
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.代码如下:
// ViewController.swift
// 数据查询器
// Created by luodev on 2022/8/11.
// Copyright 2022 luodev. All rights reserved.
import UIKit
class ViewController: UIViewController,UITableViewDataSource {
var countries:Dictionary=[
“Hh”:[“黄海”,“男”,“重庆”,“13667890123”,“QQ:678901234”,“hh@126.com”],
“Lb”:[“李斌”,“男”,“上海”,“13612345678”,“QQ:123456789”,“lb@126.com”],
“Lm”:[“李明”,“男”,“西安”,“13678901234”,“QQ:789012345”,“lm@126.com”],
“Qx”:[“钱新”,“男”,“天津”,“13645678901”,“QQ:456789012”,“qx@126.com”],
“Sl”:[“宋玲”,“女”,“北京”,“13634567890”,“QQ:345678901”,“sl@126.com”],
“Sm”:[“孙梅”,“女”,“广州”,“13689012345”,“QQ:890123456”,“sm@126.com”],
“Wy”:[“王英”,“女”,“武汉”,“13656789012”,“QQ:567890123”,“wy@126.com”],
“Xl”:[“谢璐”,“女”,“南昌”,“13690123456”,“QQ:901234567”,“xl@126.com”],
“Zq”:[“周青”,“男”,“南京”,“13623456789”,“QQ:234567890”,“zq@126.com”],
“Zj”:[“赵佳”,“女”,“北京”,“13601234567”,“QQ:012345678”,“zj@126.com”]
]
var keys:[String]=[]
//根据键名对应的键值,返回数组的长度作为制定章节的单元格数列数量
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let subCountries=countries[keys[section]]
return (subCountries?.count)!
}
//创建单元格
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier=“reusedCell”
var cell=tableView.dequeueReusableCell(withIdentifier: identifier) if(cell==nil){ cell=UITableViewCell(style:UITableViewCell.CellStyle.default,reuseIdentifier: identifier)
}
let subCountries=countries[keys[(indexPath as NSIndexPath).section]]
cell?.textLabel?.text=subCountries![(indexPath as NSIndexPath).row]
return cell!
}
//返回键名数组的键名作为表格中章节的标题文字
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return keys[section]
}
//返回键名数组作为索引序列的内容
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return keys
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
keys=Array(countries.keys).sorted()
let screenRect=UIScreen.main.bounds//获取屏幕的边界
let tableRect = CGRect(x:0,y:20,width:screenRect.size.width,height: screenRect.size.height-20)//创建矩形区域对象
let tableView=UITableView(frame:tableRect)
tableView.dataSource=self
self.view.addSubview(tableView)
}
//返回键名数组的长度作为表格中章节的数目
func numberOfSections(in tableView:UITableView)->Int{
return keys.count
}
}

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

相关内容

热门资讯

银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...