Swift自定义表格(纯代码)

1.先进行自定义cell的布局(个人喜好)

    //(1)定义属性
    var titleLab:UILabel?
    var coverImage:UIImageView?
    var desLab:UILabel?
    var priceLab:UILabel?
    //(2.)
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.lauouUI()
    }
    //(3.)布局
    coverImage = UIImageView(frame: CGRect(x: 10, y: 5, width: 100, height: 100))
        coverImage?.backgroundColor = .red
        self.addSubview(coverImage!)
        
        titleLab = UILabel(frame: CGRect(x: 120, y: 5, width: self.contentView.bounds.size.width - 130, height: 30))
        titleLab?.backgroundColor = .orange
        self.addSubview(titleLab!)
        
        priceLab = UILabel(frame: CGRect(x: 120, y: 35, width: self.contentView.bounds.size.width - 130, height: 30))
        priceLab?.backgroundColor = .yellow
        self.addSubview(priceLab!)
        
        desLab = UILabel(frame: CGRect(x: 120, y: 65, width: self.contentView.bounds.size.width - 130, height: 30))
        desLab?.backgroundColor = .blue
        self.addSubview(desLab!)
     //(4.)Model类定义的属性进行赋值
     func setValueForCell(model:Goods)  {
        
        self.titleLab?.text = model.name
        self.desLab?.text = model.desTitle
        self.priceLab?.text = model.price
        self.coverImage?.image = UIImage(named: model.coverImage!)
        
    }

2.创建表格

        let tableView = UITableView(frame: self.view.bounds, style: .plain)
 //自定义cell
        tableView.register(NSClassFromString("GoodsTableViewCell"), forCellReuseIdentifier: "goodsCell")
        tableView.dataSource = self
        tableView.delegate = self
        self.view.addSubview(tableView)
       

3.用死数据展示表格上(也可用网络数据进行替换)

        //声明一个数组接受数据(写在viewDidLoad外)
        var dataArr:Array?

        //写在viewDidLoad中即可
        let goods1 = Goods()
        goods1.name = "二娃"
        goods1.price = "¥999"
        goods1.desTitle = "千里眼"
        goods1.coverImage = "二娃"

        let goods2 = Goods()
        goods2.name = "四娃"
        goods2.price = "¥999"
        goods2.desTitle = "吐水"
        goods2.coverImage = "四娃"


        let goods3 = Goods()
        goods3.name = "五娃"
        goods3.price = "¥999"
        goods3.desTitle = "吐火"
        goods3.coverImage = "五娃"

        dataArr = [goods1,goods2,goods3]

3.实现表格数据源方法

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 150.00
    }

你可能感兴趣的:(Swift自定义表格(纯代码))