[Swift通天遁地]二、表格表单-(17)制作在表单左侧添加单选和复选组件的表单行

本文将演示如何制作在表单左侧添加单选和复选组件的表单行。

在项目导航区,打开视图控制器的代码文件【ViewController.swift】

现在开始编写代码,实现单选和复选组表单行到功能。

 1 import UIKit
 2 //首先在当前类文件中,
 3 //引入以及安装的第三方类库
 4 import Eureka
 5 
 6 //修改当前视图控制器类的父类的名称
 7 class ViewController: FormViewController {
 8     
 9     override func viewDidLoad() {
10         super.viewDidLoad()
11         
12         //创建一个字符串数组
13         let continents = ["Africa", "Antarctica", "Asia", "Australia", "Europe", "North America", "South America"]
14         
15         //在表单中添加一个可选的选择区域
16         form +++ SelectableSection<ImageCheckRow<String>>()
17         {
18             section in
19             //设置该区域头部的内容
20             section.header = HeaderFooterView(title: "Where do you live?")
21         }
22         
23         //通过一个循环,遍历字符串数组
24         for option in continents
25         {
26             //在表单的底部添加一个选择行
27             form.last! <<< ImageCheckRow<String>(option)
28             {
29                 lrow in
30                 //选择行的标题
31                 lrow.title = option
32                 //选择行可选的值
33                 lrow.selectableValue = option
34                 //选择行默认的值
35                 lrow.value = nil
36             }
37         }
38         //以上创建了一个单选区域,接着创建一个多选区域。
39 
40         //同样创建一个字符串数组
41         let oceans = ["Arctic", "Atlantic", "Indian", "Pacific", "Southern"]
42         
43         //在表单中添加一个可选的选择区域
44         //并设置选择区域的类型为多项选择
45         form +++ SelectableSection<ImageCheckRow<String>>("And which of the following oceans have you taken a bath in?",
46                                                           selectionType: .multipleSelection)
47         //通过一个循环,遍历字符串数组
48         for option in oceans
49         {
50             //在表单的底部添加一个选择行
51             form.last! <<< ImageCheckRow<String>(option)
52             {
53                 lrow in
54                 //选择行的标题
55                 lrow.title = option
56                 //选择行可选的值
57                 lrow.selectableValue = option
58                 //选择行默认的值
59                 lrow.value = nil
60             }
61             //对单元格执行设置操作
62             .cellSetup
63             {
64                 cell, _ in
65                 //设置单元格被选中时的标识图片
66                 cell.trueImage = UIImage(named: "selectedRectangle")!
67                 //设置单元格未被选中时的标识图片
68                 cell.falseImage = UIImage(named: "unselectedRectangle")!
69             }
70         }
71     }
72     
73     override func didReceiveMemoryWarning() {
74         super.didReceiveMemoryWarning()
75         // Dispose of any resources that can be recreated.
76     }
77 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10202440.html