iOS学习十二之选择器控件UIPickerView
UIPickerView是一个简易的列表控件,用于提供有限个数的选项供用户选择。
它是通过代理和数据源的方法对其进行设置和数据源填充的,这种控件的设计模式也是代理模式的应用之一。
添加下面的代码即可实现基本功能。
class ViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let picker = UIPickerView(frame:CGRect(x:20, y:100, width:500, height:150))
picker.delegate = self
picker.dataSource = self
self.view.addSubview(picker)
}
//设置视图的分区数,也可以理解为选择列表的列数
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
//设置每个分区的行数,参数component用于判断具体的分区
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 8
}
//返回设置列表中每一行的数据,参数row和component分别用于区分行和列
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return "\(component)area\(row)line data"
}
//返回值设置具体行的行高
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 50
}
//返回值设置分区的宽度,即列的宽度
func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
return 200
}
//选中数据时的回调代理
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("\(component)area\(row)line data")
}
最新文章
- Lua 调用 Opencv 的方法
- [工作积累] OpenGL ES3.0: glInvalidateFramebuffer
- [杂题]URAL1822. Hugo II's War
- iOS开发经验总结(下)
- Java订单号(时间加流水号)
- 查看当前支持的shell,echo -e相关转义符,一个简单shell脚本,dos2unix命令把windows格式转为Linux格式
- springboot+aop切点记录请求和响应信息
- domain logic approaches
- Mac无法清倒废纸篓,终极解决方案
- 去掉vim的BELL提示音
- python---redis的python使用
- solr7.4 安装与使用
- PSP(3.23——3.29)以及周记录
- 【机器学习】Softmax 和Logistic Regression回归Sigmod
- github/gitlab添加多个ssh key
- maven第二天——重要概念与其它操作
- HDU1233 还是畅通工程 2017-04-12 19:49 64人阅读 评论(0) 收藏
- 湖南大学ACM程序设计新生杯大赛(同步赛)E - Permutation
- 关于alter database open resetlogs及incarnation的一点理解
- windows RT开发笔记:WinRT DLL及其调用研究
热门文章
- SQL注入之延时注入(10)
- Java基础——IO模型详解
- [部署日记]Android Studio在同步Gradle的时候提示Could not resolve com.android.tools.build:gradle:7.0.4
- debian安装mate桌面环境的方法(图)
- 自动化:web网页理解
- c++中内联函数和宏函数的区别
- JAVA格式化数字字符串,如手机号,银行卡号的格式化
- 思科vlan简单配置
- 连接Oracle 19c出现ORA-28040:没有匹配的验证协议
- 配置PostMan以访问、连接D365 / Dataverse环境