8.2、自定义UItableViewCell及闭包使用

import UIKit
class Two: UIView,UITableViewDelegate,UITableViewDataSource {
    let ScreenWidth = UIScreen.main.bounds.width
    let ScreenHeight = UIScreen.main.bounds.height
    var twoTableView = UITableView()
    func createTwoTableView() {
        let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight), style: .plain)
        self.addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
        self.twoTableView = tableView
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 10
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 140
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = TwoTableViewCell.init(style: .default, reuseIdentifier: "two")
        cell.changeBlock = {
            (values) in
            var num:Int = values
            num += 1
            cell._numstr = num
            cell.numLabel.text = "\(num)元/kg"
        }
        return cell
    }
}

//
//  TwoTableViewCell.swift
//  两个tableview
//
//  Created by modai on 2017/7/26.
//  Copyright © 2017年 huanqiu. All rights reserved.
//

import UIKit

class TwoTableViewCell: UITableViewCell {
    let ScreenWidth = UIScreen.main.bounds.width
    typealias changeClick = (Int)->()
    var changeBlock:changeClick?

    var leftImage = UIImageView()
    var titleLabel = UILabel()
    var desLabel = UILabel()
    var numLabel = UILabel()
    var addBtn = UIButton()
    
    var _numstr:Int = 90
    var numstr:Int{
        set{
            _numstr = newValue
        }
        get{
            return _numstr
        }
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUpUI()
    }
//    当我们使用storyboard实现界面的时候,程序会调用这个初始化器。
//    注意要去掉fatalError,fatalError的意思是无条件停止执行并打印。
//    在obj-c中可以通过下面代码实现
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    func setUpUI() {
        leftImage = UIImageView(frame: CGRect(x: 10, y: 10, width: 110, height: 110))
        self.addSubview(leftImage)
        leftImage.image = #imageLiteral(resourceName: "headImages")
        leftImage.clipsToBounds = true
        leftImage.contentMode = .scaleAspectFit
        leftImage.layer.cornerRadius = ScreenWidth/3/2-5
        titleLabel = UILabel(frame: CGRect(x: ScreenWidth/3, y: 0, width: ScreenWidth-ScreenWidth/3, height: 30))
        self.addSubview(titleLabel)
        titleLabel.text = "猕猴桃-能量巨果,一口即爆"
        titleLabel.font = UIFont.systemFont(ofSize: 18)
        
        desLabel = UILabel(frame: CGRect(x: ScreenWidth/3, y: 40, width: ScreenWidth-ScreenWidth/3-10, height: 70))
        self.addSubview(desLabel)
        desLabel.text = "新西兰进口水果,新鲜高营养,买的是放心,吃的是健康,此生绝不能错过的一大优品,走过路过不要错过啊,过了这村可没这店了"
        desLabel.font = UIFont.systemFont(ofSize: 14)
        desLabel.numberOfLines = 0
        desLabel.textColor = UIColor.lightGray
        
        numLabel = UILabel(frame: CGRect(x: ScreenWidth/3, y: 120, width: ScreenWidth/3, height: 20))
        self.addSubview(numLabel)
        numLabel.text = "\(_numstr)元/kg"
        numLabel.textColor = UIColor.red
        numLabel.font = UIFont.systemFont(ofSize: 14)
        
        addBtn = UIButton(frame: CGRect(x: ScreenWidth-60, y: 120, width: 40, height: 20))
        self.addSubview(addBtn)
        addBtn.setTitle("+", for: .normal)
        addBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        addBtn.setTitleColor(UIColor.red, for: .normal)
        addBtn.addTarget(self, action: #selector(changeAction), for: .touchUpInside)
    }
    func changeAction() {
        if (self.changeBlock != nil) {
            self.changeBlock!(_numstr)
        }
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

你可能感兴趣的:(8.2、自定义UItableViewCell及闭包使用)