[Swift通天遁地]二、表格表单-(1)创建自定义的UITableViewCell(单元格类)

本文将演示如何在表单视图中,添加一个自定义的单元格类。

首先创建一个自定义的单元格类。

在项目文件夹【DemoApp】上点击鼠标右键,弹出右键菜单。

【New File】->【Cocoa Touch Class】->【Next】->

【Class】:CustomizeUITableViewCell ,类名。

【Subclass of】:UITableViewCell ,父类

【Language】:Swift

->【Next】->【Create】

在项目导航区,打开刚刚创建的代码文件【CustomizeUITableViewCell.swift】

现在开始编写代码,往自定义单元格中,添加一些控件。

 1 import UIKit
 2 
 3 class CustomizeUITableViewCell: UITableViewCell
 4 {
 5     //该单元格拥有三个子元素,分别是:
 6     //1.左侧的缩略图
 7     var thumbnail : UIImageView!
 8     //2.中间的标题
 9     var title : UILabel!
10     //3.右侧的细节按钮
11     var detail : UIButton!
12     
13     //重写单元格的自定义方法,在该方法中对单元格进行自定义操作
14     override init(style: UITableViewCellStyle, reuseIdentifier: String?)
15     {
16         //首先实现父类的初始化方法
17         super.init(style: style, reuseIdentifier: reuseIdentifier);
18         
19         //初始化缩略图对象,用来显示项目中的一张图片
20         self.thumbnail = UIImageView(image: UIImage(named: "user"))
21         //设置缩略图在单元格中的显示区域,位于单元格的左侧
22         self.thumbnail.frame = CGRect(x: 20, y: 10, width: 24, height: 24)
23         
24         //初始化标题标签,并设置该标签的显示区域
25         self.title = UILabel(frame: CGRect(x: 80, y: 0, width: 120, height: 44))
26         //设置标签的文字内容
27         self.title.text = ""
28         //设置标签字体的外观属性
29         self.title.font = UIFont(name: "Arial", size: 15)
30         
31         //初始化细节按钮控件,并设置按钮的显示区域,位于单元格的右侧
32         self.detail = UIButton(frame: CGRect(x: 240, y: 12, width: 60, height: 20))
33         //设置按钮在正常状态下的标题文字
34         self.detail.setTitle("Detail", for: UIControlState())
35         //设置按钮标题文字的字体属性
36         self.detail.titleLabel?.font = UIFont(name: "Arial", size: 13)
37         //设置按钮的背景颜色为橙色
38         self.detail.backgroundColor = UIColor.orange
39         //设置按钮的层的圆角半径为10,从而创建一个圆角按钮。
40         self.detail.layer.cornerRadius = 10
41         //给按钮控件绑定点击事件
42         self.detail.addTarget(self, action: #selector(CustomizeUITableViewCell.showDetail(_:)), for: UIControlEvents.touchUpInside)
43         
44         //依次将三个控件添加到单元格中
45         self.addSubview(self.thumbnail)
46         self.addSubview(self.title)
47         self.addSubview(self.detail)
48     }
49     
50     //添加一个方法,用来响应细节按钮的点击事件
51     func showDetail(_ sender:UIButton)
52     {
53         print("Detail Informaiton.")
54     }
55     
56     //添加一个必须实现的初始化方法
57     required init(coder aDecoder: NSCoder)
58     {
59         fatalError("init(code:)has not brrn implomented");
60     }
61 }

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

现在开始创建一个表格,并在表格中使用刚刚创建的单元格。

 1 import UIKit
 2 
 3 //使当前的视图控制器类,遵循表格的数据源协议UITableViewDataSource
 4 class ViewController: UIViewController, UITableViewDataSource {
 5 
 6     override func viewDidLoad() {
 7         super.viewDidLoad()
 8         // Do any additional setup after loading the view, typically from a nib.
 9         
10         //获得设备的屏幕尺寸
11         let screenRect = UIScreen.main.bounds
12         //创建一个矩形区域,作为表格视图的显示区域。
13         let tableRect = CGRect(x: 0, y: 20, width: screenRect.size.width, height: screenRect.size.height - 20)
14         //初始化一个指定显示区域的表格对象
15         let tableView = UITableView(frame: tableRect)
16         
17         //设置表格对象的数据源为当前的视图控制器对象
18         tableView.dataSource = self
19         //并将表格视图添加到当前的视图控制器的根视图中
20         self.view.addSubview(tableView)
21     }
22     
23     //添加一个代理方法,用来设置表格的行数
24     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
25     {
26         //在此设置表格拥有20个单元格
27         return 20
28     }
29     
30     //添加一个代理方法,用来初始化或复用表格中的单元格
31     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
32     {
33         //创建一个字符串常量,作为单元格的复用标识
34         let identifier = "reusedCell"
35         //根据复用标识从表格中获取可以复用的单元格
36         var cell:CustomizeUITableViewCell? = tableView.dequeueReusableCell(withIdentifier: identifier) as? CustomizeUITableViewCell
37         
38         //如果没有可以复用的单元格
39         if(cell == nil)
40         {
41             //则初始化一个自定义的单元格,并设置单元格的复用标识。
42             cell = CustomizeUITableViewCell(style: UITableViewCellStyle.default, 
43                                             reuseIdentifier: identifier)
44         }
45         
46         //设置自定义单元格的标题文字
47         //当然也可以设置单元格的缩略图和细节按钮的相关属性
48         cell?.title?.text = "User Name"
49         return cell!
50     }
51     
52     override func didReceiveMemoryWarning() {
53         super.didReceiveMemoryWarning()
54         // Dispose of any resources that can be recreated.
55     }
56 }

猜你喜欢

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