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
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,025评论 3 119
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,964评论 25 709
  • 用户输入内容 控制台 文件 我们可以创建一个file类对象,通过它的read 、readline、write等方法...
    ZYiDa阅读 142评论 0 1
  • 我们都太喜欢等,固执地相信等待永远没有对错,美好的岁月就这样被一个又一个等待消耗掉。没有在最重要的时候去做想做的事...
    厚厚的三明治阅读 175评论 0 0
  • 挂在梦乡的月亮 □巴山雪儿 你从哪里来 路过 ...
    巴山雪儿阅读 207评论 2 7