美文网首页
8.2、自定义UItableViewCell及闭包使用

8.2、自定义UItableViewCell及闭包使用

作者: 艾希_可可 | 来源:发表于2018-06-27 11:53 被阅读27次
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及闭包使用

  • iOS开发之Swift篇(7)—— 闭包

    目录 版本 闭包的定义及使用 尾随闭包 逃逸闭包 版本 Xcode 11.0Swift 5.1 闭包的定义及使用 ...

  • tabelViewCell

    UITableViewCell: 1.使用系统自定义的各种UITableViewCell的样式 - (UITabl...

  • 使用闭包

    使用闭包自定义排序 当你开始深入研究集合 (包括数组,字典,集合set)时,闭包就派上了用场。之前,我们使用数组的...

  • 闭包及使用

    闭包类型 cell中使用时例子 下面是控制器中使用例子

  • groovy-闭包

    定义和执行闭包 闭包作为参数返回 闭包作为参数传递 闭包使用外部变量 注意: 闭包可使用(引用)闭包外部定义的变量...

  • UITableViewCell的高度那些事

    在项目开发中,我们经常会使用到UITableViewCell,不管是我们自定义UITableViewCell还是使...

  • UITableViewCell常用方法说明

    UITableViewCell使我们经常使用的,在开发中我们经常会通过自定义UITableViewCell来实现各...

  • laravel-路由规则

    自定义路由逻辑 如果想要使用自定义的路由解析规则,可以使用Route:bind方法。传入bind方法中的闭包函数(...

  • Gradle开发-Groovy闭包

    # 闭包 闭包的基础知识 闭包的使用 闭包 this,owner,delegate 的理解 总结 ## 闭包的基础...

网友评论

      本文标题:8.2、自定义UItableViewCell及闭包使用

      本文链接:https://www.haomeiwen.com/subject/kpjbyftx.html