给 cell 传值时,直接将 model 传给 cell

1、使用第三方库 MJExtension 将字典转模型

2、在 cellForRowAtIndexPath: 中将 AppModel 创给 MyTableViewCell, 然后在 MyTabelViewCell 里面对 cell 内的每个控件进行赋值

3、MyTabelViewCell 类需要重写init(style: UITableViewCellStyle, reuseIdentifier resueIdentifier: String?){}方法

4、直接将 model 创给自定义 cell, 然后在自定义 cell 里面进行赋值,这样避免了cellForRowAtIndexPath:代码过多

代码:

ViewController.swift:

 1 import UIKit
 2 import MJExtension
 3 class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
 4     lazy var tableView: UITableView = {
 5         
 6         let tableView: UITableView!
 7         
 8         tableView = UITableView(frame: self.view.frame, style: .plain)
 9         tableView.delegate = self
10         tableView.dataSource = self
11         
12         return tableView
13     }()
14     
15 //    lazy
16     
17 //    var items = ["北京", "上海", "广东", "深圳", "杭州"]
18     var items = [["imageName": "1", "appName": "Football Maze", "appDescription": "足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球"],
19                  ["imageName": "1", "appName": "租房点评", "appDescription": "足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球足球"],
20                  ["imageName": "1", "appName": " iJump", "appDescription": "运动运动运动运动运动运动运动运动运动运动运动运动运动运动运动运动运动运动运动运动运动运动运动运动运动运动运动运动运动运动"],
21                  ["imageName": "1", "appName": "哪里逃", "appDescription": "跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路"]]
22     
23     override func viewDidLoad() {
24         super.viewDidLoad()
25         
26         // 将tableView 添加到 view 上
27         self.view.addSubview(self.tableView)
28         
29 //        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "编辑", style: .done, target: self, action: #selector(ViewController.editButtonClick))
30         
31 //        self.navigationItem.sel
32     }
33     
34     override func didReceiveMemoryWarning() {
35         super.didReceiveMemoryWarning()
36         // Dispose of any resources that can be recreated.
37     }
38     
39 //    MARK: -UITableViewDelegate, UITableViewDataSource
40     // tableView 数据源: 返回几个组
41     func numberOfSections(in tableView: UITableView) -> Int {
42         return 1
43     }
44     
45     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
46         
47         let cellIdentifier = "cellIdentifier"
48         var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? MyTableViewCell
49         
50         if cell == nil {
51             cell = MyTableViewCell(style: .subtitle, reuseIdentifier: cellIdentifier)
52             
53             // 设置 cell 字体
54             cell?.textLabel?.font = UIFont.systemFont(ofSize: 14)
55             
56             // 设置选中 cell 样式
57             cell?.selectionStyle = .gray
58             
59             // 设置 cell 后面的箭头样式
60             cell?.accessoryType = .disclosureIndicator
61             
62         }
63         
64         // 设置 cell 的内容
65 //        cell?.textLabel?.text = items[indexPath.row]
66         
67         // 设置 cell 图片
68 //        cell?.imageView?.image = UIImage(named: "1")
69         
70 //        cell?.detailTextLabel?.text = "详细信息介绍"
71         let cellModel: AppsModel = AppsModel.mj_object(withKeyValues: self.items[indexPath.row]) // 使用 MJExtension 将字典转模型
72         
73         // 通过自定义方法给 cell 赋值
74         cell?.showAppInfoWithModel(model: cellModel)
75         
76         return cell!
77     }
78   
79     // 设置行高
80     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
81         return 60
82     }
83 }

 

 MyTableViewCell.swift:

 1 import UIKit
 2 class MyTableViewCell: UITableViewCell {
 3     var iconImageView: UIImageView! // 图片
 4     var appNameLabel: UILabel! // 标题
 5     var descriptionLabel: UILabel! // 描述
 6     
 7     // 赋值方法 - 显示 cell 内容方法
 8     func showAppInfoWithModel(model: AppsModel) {
 9         // 获取 model 中的图片
10         iconImageView.image = UIImage(named: model.imageName!)
11         
12         // 获取 model 中的App 名字
13         appNameLabel.text = model.appName
14         
15         // 获取 model 中的 App 描述
16         descriptionLabel.text = model.appDescription
17     }
18     
19     override init(style: UITableViewCellStyle, reuseIdentifier resueIdentifier: String?) {
20         super.init(style: style, reuseIdentifier: resueIdentifier)
21         
22         // 创建 iconImageView
23         iconImageView = UIImageView(frame: CGRect(x: 10, y: 5, width: 40, height: 40))
24         self.addSubview(iconImageView)
25         
26         // 创建 appNameLabel
27         appNameLabel = UILabel(frame: CGRect(x: 60, y: 0, width: 220, height: 15))
28         appNameLabel.font = UIFont.systemFont(ofSize: 16)
29         self.addSubview(appNameLabel)
30         
31         // 创建 desctiptionLabel
32         descriptionLabel = UILabel(frame: CGRect(x: 60, y: 15, width: 220, height: 35))
33         descriptionLabel.font = UIFont.systemFont(ofSize: 12)
34         descriptionLabel.numberOfLines = 2
35         descriptionLabel.textColor = UIColor.lightGray
36         self.addSubview(descriptionLabel)
37     }
38     
39     required init?(coder aDecoder: NSCoder) {
40         fatalError("init(coder:) has not been implemented")
41     }
42     
43     override func awakeFromNib() {
44         super.awakeFromNib()
45         // Initialization code
46     }
47     override func setSelected(_ selected: Bool, animated: Bool) {
48         super.setSelected(selected, animated: animated)
49         // Configure the view for the selected state
50     }
51 }

AppsModel.swift:

1 import UIKit
2 
3 class AppsModel: NSObject {
4     
5     var imageName: String?
6     var appName: String?
7     var appDescription: String?
8 }

 

转载于:https://www.cnblogs.com/XuChao-Zs/p/6170886.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值