UIKit - 自定义tableViewCell

tableView的数据样式主要由tableViewCell来控制,所以我们通过重新定制tableViewCell的样式来达到我们想要的效果!

1.创建自定义tableViewCell(tableViewCell.swift)

import UIKit

class GoodsTableViewCell: UITableViewCell {
    
    //定义使用到的变量
    var photo: UIImageView?
    var titleLabel:UILabel?
    var desLabel:UILabel?
    var priceLabel:UILabel?
    var timeLabel:UILabel?

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
   
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.cellView()
    }
    
    //设置TableCell样式
    func cellView(){
        titleLabel = UILabel(frame: CGRect(x: 130, y: 5, width: 300, height: 30))
        self.addSubview(titleLabel!)
        
        desLabel = UILabel(frame: CGRect(x: 130, y: 40, width: 300, height: 30))
        self.addSubview(desLabel!)
        
        priceLabel = UILabel(frame: CGRect(x: 130, y: 80, width: 100, height: 30))
        self.addSubview(priceLabel!)
        
        timeLabel = UILabel(frame: CGRect(x: 300, y: 80, width: 100, height: 30))
        self.addSubview(timeLabel!)
        
        photo = UIImageView(frame:CGRect(x: 20, y: 5, width: 100, height: 100))
        self.addSubview(photo!)
    }
    
    //设置TableCell数据
    func setValueForCell(cellData:Dictionary<String,Any>){
        self.titleLabel?.text = cellData["name"] as! String?
        self.titleLabel?.textColor = UIColor.blue
        
        self.desLabel?.text = cellData["desc"] as! String?
        
        self.priceLabel?.text = cellData["price"] as! String?
        self.priceLabel?.textColor = UIColor.red
        
        self.timeLabel?.text = cellData["time"] as! String?
        
        self.photo?.image = UIImage(named:cellData["image"] as! String!)
    }
    
    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
    }

}

2.将表格添加到当前视图中(viewController.swift)

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{

    //数据源
    let goodsItem = [
            ["name":"茶杯犬","desc":"茶杯犬详情描述","price":"1500.00","image":"image1","time":"2018/08/01"],
            ["name":"折耳猫","desc":"折耳猫详情描述","price":"2000.00","image":"image2","time":"2018/08/02"],
            ["name":"兔子","desc":"折耳猫详情描述","price":"2500.00","image":"image3","time":"2018/08/05"],
            ["name":"龙猫","desc":"龙猫详情描述","price":"2500.00","image":"image3","time":"2018/08/10"],
            ["name":"仓鼠","desc":"仓鼠详情描述","price":"2500.00","image":"image3","time":"2018/08/11"],
            ["name":"鹦鹉","desc":"鹦鹉详情描述","price":"2500.00","image":"image4","time":"2018/08/12"]
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //添加tableView
        let tableView = UITableView(frame: self.view.bounds, style: .plain)
        self.view.addSubview(tableView)
        
        //注册GoodsTableViewCell类
        tableView.register(NSClassFromString("GoodsTableViewCell"), forCellReuseIdentifier:
            "goodsCell")
        
        //设置tableView数据与代理
        tableView.delegate = self
        tableView.dataSource = self
        // Do any additional setup after loading the view, typically from a nib.
    }

    //设置行数
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return goodsItem.count
    }
    
    //返回新的TableCell样式
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //实例化新样式
        let cell = GoodsTableViewCell(style: .default, reuseIdentifier: "goodsCell")
        
        //设置数据
        cell.setValueForCell(cellData: goodsItem[indexPath.row])
        return cell
    }
    
    //设置行高
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 120.00
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
        // Dispose of any resources that can be recreated.
    }

}

3.文件目录结构

4.最终效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值