swift 公共列表的再次使用

09/17 08:15
阅读数 12

之前定义过公共列表

只需要重新定义cell

//
//  DealListCell.swift
//  geekTimeSwiftUI
//
//  Created by Ben Lv on 2020/2/8.
//  Copyright © 2020 ruibo. All rights reserved.
//

import Foundation
import UIKit
import SnapKit

class DealListCell: CommonListCell<Deal> {
    
    let progressLabel: UILabel
    let productImageView: UIImageView
    
    override var item: Deal? {
        didSet{
            if let p = self.item {
                self.productImageView.kf.setImage(with: URL(string: p.product.imageUrl))
                self.textLabel?.text = p.product.name
                self.detailTextLabel?.text = p.product.desc
                self.progressLabel.text = "已经学习 \(p.progress)%"
            }
        }
    }
    
    required init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        progressLabel = UILabel(frame: .zero)
        productImageView = UIImageView()
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setupView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupView() {
        textLabel?.textColor = UIColor.hexColor(0x333333)
        detailTextLabel?.textColor = UIColor.hexColor(0x999999)
        detailTextLabel?.numberOfLines = 2
        progressLabel.textColor = UIColor.hexColor(0xe23b41)
        progressLabel.font = UIFont.systemFont(ofSize: 15)
        productImageView.contentMode = .scaleAspectFill
        productImageView.clipsToBounds = true
        contentView.addSubview(progressLabel)
        contentView.addSubview(productImageView)
        
        
        productImageView.snp.makeConstraints { (make) in
            make.left.equalTo(contentView).offset(20)
            make.top.equalTo(contentView).offset(10)
            make.width.equalTo(80)
            make.height.equalTo(100)
        }
        
        textLabel?.snp.makeConstraints({ (make) in
            make.left.equalTo(productImageView.snp_right).offset(12)
            make.top.equalTo(productImageView)
            make.right.equalTo(contentView).offset(-20)
        })
        
        progressLabel.snp.makeConstraints { (make) in
            make.left.equalTo(textLabel!)
            make.centerY.equalTo(contentView)
        }
        
        detailTextLabel?.snp.makeConstraints({ (make) in
            make.left.equalTo(textLabel!)
            make.bottom.equalTo(productImageView)
            make.right.equalTo(contentView).offset(-20)
        })
        
        
        
        
    }
    
    
    
}


然后再次使用即可

//
//  DealListViewController.swift
//  geekTime
//
//  Created by liuan on 2020/9/16.
//  Copyright © 2020 liuan. All rights reserved.
//

import UIKit

class DealListViewController: BaseViewController,CommonListDelegate {
    func didSelectItem<Item>(_ item: Item) {
        if let deal = item as? Deal {
         
            let detailVC = DetailViewController()
            detailVC.product = deal.product
            navigationController?.pushViewController(detailVC, animated: true)
        }
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let productList = CommonList<Deal,DealListCell>()
               productList.items = FakeData.createDeals()
               productList.delegate = self
               view.addSubview(productList)
               productList.snp.makeConstraints({(make) in
                make.edges.equalToSuperview()
                
               } )
    }
    


}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
在线直播报名
返回顶部
顶部