table进阶--自定义cell

原单元格
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        cell.textLabel?.text = areas[indexPath.row]
        cell.imageView?.image = UIImage(named: areaImages[indexPath.row])
        
        return cell
    }

as 类型转换,从一个类型转换到另一个类型
as! 强制转换,失败app会崩溃
as? 安全转换,失败了不会崩溃

自定义单元格
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell

        cell.nameLab.text = areas[indexPath.row]
        cell.thumbImage.image = UIImage(named: areaImages[indexPath.row])
        cell.proviceLab.text = provinces[indexPath.row]
        cell.partLab.text = parts[indexPath.row]
        
        cell.thumbImage.layer.cornerRadius = cell.thumbImage.frame.size.width/2
        cell.thumbImage.layer.masksToBounds = true
        
        return cell
    }

你可能感兴趣的:(table进阶--自定义cell)