swift纯代码自定义UITableViewCell —— Swift

swift纯代码自定义UITableViewCell —— Swift

今天学习一下用swift纯代码进行自定义UITableViewCell顺便自己做个笔记,其实用swift自定义tableViewCell的思想和Object-c是一样的,画重点那就直接上代码了:

自定义tableViewCell的部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import UIKit
 
class FirstCustomTableCell: UITableViewCell {
 
    let imageView_W = 120.0//w:h = 4:3
    let imageView_H = 90.0
    let subView_interval:CGFloat = 10.0
     
    var leftImageView : UIImageView?
    var nameLabel     : UILabel?
    var subNameLabel  : UILabel?
    var timeLabel     : UILabel?
    var browseLabel   : UILabel?
     
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCellSelectionStyle.none
         
        self.createCellUI()
    }
     
    func createCellUI(){
         
        leftImageView = UIImageView.init(frame : CGRect(x:15.0,y:5.0,width:imageView_W,height:imageView_H))
        leftImageView!.backgroundColor = UIColor.lightGray
        self.contentView.addSubview(leftImageView!)
        leftImageView?.image = UIImage(named: "leftImg1.jpg")
         
        //name
        nameLabel = UILabel.init(frame: CGRect(x:Max_X(object:leftImageView!) + subView_interval,y:subView_interval,width:SCREEN_WIDTH - Max_X(object:leftImageView!) - 2 * subView_interval,height:25.0))
        nameLabel?.textColor = UIColor.darkText
        nameLabel?.font = UIFont.systemFont(ofSize: 18)
        nameLabel?.text = "世界杯开幕";
        self.contentView.addSubview(nameLabel!)
         
        subNameLabel = UILabel.init(frame: CGRect(x:X(object:nameLabel!),y:Max_Y(object: nameLabel!) + 5,width:W(object: nameLabel!),height:25))
        subNameLabel?.textColor = UIColor.darkGray
        subNameLabel?.font = UIFont.systemFont(ofSize: 15)
        subNameLabel?.text = "世界杯开幕";
        self.contentView.addSubview(subNameLabel!)
         
        timeLabel = UILabel.init(frame: CGRect(x:X(object:nameLabel!),y:100-25,width:W(object: subNameLabel!) * 0.6,height:20))
        timeLabel?.textColor = UIColor.lightGray
        timeLabel?.font = UIFont.systemFont(ofSize: 13)
        timeLabel?.text = "2018-01-01 10:58"
        self.contentView.addSubview(timeLabel!)
         
        browseLabel = UILabel.init(frame: CGRect(x:Max_X(object: timeLabel!),y:Y(object: timeLabel!),width:W(object: subNameLabel!) * 0.4,height:20))
        browseLabel?.textAlignment = NSTextAlignment.right
        browseLabel?.textColor = UIColor.lightGray
        browseLabel?.font = UIFont.systemFont(ofSize: 13)
        browseLabel?.text = "浏览:"+"50"
        self.contentView.addSubview(browseLabel!)
    }
     
    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
    }
 
}

在VC中对cell进行注册:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import UIKit
 
class FirstCustomTableCellVc: UIViewController,UITableViewDelegate,UITableViewDataSource {
 
    let cell_identifier:String = "FirstCustomTableCell"
    var myTableView = UITableView()
 
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.navigationItem.title = "自定义TableViewCell"
        self.creatViewUI()
    }
     
    func creatViewUI(){
         
        self.myTableView = UITableView.init(frame: self.view.bounds, style: UITableViewStyle.plain)
        self.myTableView.tableFooterView = UIView.init()
        self.myTableView.delegate = self
        self.myTableView.dataSource = self
        self.view .addSubview(self.myTableView)
         
        self.myTableView.register(FirstCustomTableCell.classForCoder(), forCellReuseIdentifier: cell_identifier)
    }
     
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         
        return 10
    }
     
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
     
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         
        let customCell = tableView.dequeueReusableCell(withIdentifier: cell_identifier, for: indexPath)
         
        return customCell
    }
     
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
     
 
    /*
    // 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.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
 
}

 最后实现的效果如下图:

 

 代码中用到的几个自定义的获取view的frame的函数是我进行的一个简单的自定义可以参考下面的文件,后续会用swift专门写一个类目用于view的frame参数的获取和重置:

 FrameUtils.swift.zip

posted @   #零下一度&  阅读(7269)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示