美文网首页
iOS Swift4.0 自定义表格UITableViewCel

iOS Swift4.0 自定义表格UITableViewCel

作者: rainbow_H | 来源:发表于2018-08-24 16:51 被阅读0次

var dataArr:Array?

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

        self.view.addSubview(tableView)

//    注册自定义cell

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

        tableView.delegate=self

        tableView.dataSource=self

 functableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{

return dataArr!.count

}

 functableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{

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

//    添加数据 

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

    returncell

}

创建继承于UITableViewCell的类

classGoodsTableViewCell:UITableViewCell{

//  1、定义属性

    vartitleLab:UILabel?

    varcoverImageView:UIImageView?

    vardesLab:UILabel?

    varpriceLab:UILabel?

    requiredinit?(coder aDecoder:NSCoder) {

        super.init(coder: aDecoder)

    }

//  2、初始化子视图

    overrideinit(style:UITableViewCellStyle, reuseIdentifier:String?) {

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

        self.lauoutUI()

    }

    funclauoutUI()  {

        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!)

    }

    funcsetValueForCell(model:Goods){

        self.titleLab?.text= model.name

        self.priceLab?.text= model.price

        self.desLab?.text= model.desTitle

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

    }

相关文章

网友评论

      本文标题:iOS Swift4.0 自定义表格UITableViewCel

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