Swift_UI:(八)、UITableView自定义Cell

创建基于NSObject的Goods类:

import UIKit


class Goods: NSObject {

    var name:String?

    var price:String?

    var coverIamge:String?

    var desTitle:String?

}

新建基于UITableViewCell的类GoodsTableViewCell:

import UIKit


class GoodsTableViewCell: UITableViewCell {

    var titleLab:UILabel?

    var coverImageView:UIImageView?

    var desLab:UILabel?

    var priceLab:UILabel?

    

    required init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)

    }

    

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.lauoutUI()

        

    }

    

    func lauoutUI()  {

        coverImageView = UIImageView(frame:CGRect(x:10,y:5,width:100,height:100))

        self.addSubview(coverImageView!)

        

        titleLab = UILabel(frame:CGRect(x:120,y:5,width:self.contentView.bounds.size.width - 130,height:30))

        self.addSubview(titleLab!)

        

        priceLab = UILabel(frame:CGRect(x:120,y:35,width:self.contentView.bounds.size.width - 130,height:30))

        self.addSubview(priceLab!)

        

        desLab = UILabel(frame:CGRect(x:120,y:65,width:self.contentView.bounds.size.width - 130,height:30))

        self.addSubview(desLab!)

    }

    

    func setValueForCell(model:Goods){

        self.titleLab?.text = model.name

        self.priceLab?.text = model.price

        self.desLab?.text = model.desTitle

        self.coverImageView?.image = UIImage(named:model.coverIamge!)

    }

    

    

    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

    }


}


在ViewController.swift文件中实现:

import UIKit


class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{

    var dataArr:Array<Goods>?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        let goods1 = Goods()

        goods1.name = "西凤酒-华山论剑"

        goods1.price = "300.00"

        goods1.desTitle = "西凤酒是中国四大名酒之一,曾荣获过万国博览会金奖。华山论剑西凤酒,中国峰峻品格的首倡者。"

        goods1.coverIamge = "image"

    

        let goods2 = Goods()

        goods2.name = "西凤酒-华山论剑"

        goods2.price = "300.00"

        goods2.desTitle = "西凤酒是中国四大名酒之一,曾荣获过万国博览会金奖。华山论剑西凤酒,中国峰峻品格的首倡者。"

        goods2.coverIamge = "image"

    

        let goods3 = Goods()

        goods3.name = "西凤酒-华山论剑"

        goods3.price = "300.00"

        goods3.desTitle = "西凤酒是中国四大名酒之一,曾荣获过万国博览会金奖。华山论剑西凤酒,中国峰峻品格的首倡者。"

        goods3.coverIamge = "image"

    

        dataArr = [goods1,goods2,goods3]

        

        let tableView = UITableView(frame:self.view.bounds,style:.plain)

        self.view.addSubview(tableView)

     tableView.register(NSClassFromString("GoodsTableViewCell"), forCellReuseIdentifier: "goodsCell")

        tableView.delegate = self

        tableView.dataSource = self

    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return dataArr!.count

    }

    

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = GoodsTableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "goodsCell")

    

        cell.setValueForCell(model: dataArr![indexPath.row])

        return cell

    }

    

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        return 100.00

    }

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}


你可能感兴趣的:(Swift开发)