Swift基础入坑

table进阶--自定义cell

2018-06-11  本文已影响10人  iOS_July
原单元格
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
    }
上一篇 下一篇

猜你喜欢

热点阅读