ios - 尝试将自定义单元格添加到表格视图中时出错
问题描述
我正在尝试将自定义单元格添加到我的表格视图中
这是我为自定义CustomProfileView.xib文件配置的
主故事板
我不断得到
使用未声明的类型“CustomProfileCell”
这是我的整个代码
//
// FirstViewController.swift
// tabbed
//
// Created by Ben Heng on 7/18/18.
// Copyright © 2018 LR Web Design. All rights reserved.
//
import UIKit
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var profileTableView: UITableView!
var profileArray = [Profile]()
override func viewDidLoad() {
super.viewDidLoad()
profileTableView.delegate = self
profileTableView.dataSource = self
profileTableView.register(UINib(nibName: "ProfileCell",bundle: nil) , forCellReuseIdentifier: "CustomProfileCell")
retrieveProfiles()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return profileArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customProfileCell", for: indexPath) as! CustomProfileCell
cell.profileImage.image = UIImage(named: profileArray[indexPath.row].profileImage)
cell.profileName.text = profileArray[indexPath.row].name
cell.deviceCount.text = profileArray[indexPath.row].deviceCount
return (cell)
}
func retrieveProfiles() {
let p1 = Profile()
p1.name = "John"
p1.deviceCount = "10"
p1.profileImage = "john"
profileArray.append(p1)
let p2 = Profile()
p2.name = "Jane"
p2.deviceCount = "5"
p2.profileImage = "jane"
profileArray.append(p2)
let p3 = Profile()
p3.name = "Andrew"
p3.deviceCount = "4"
p3.profileImage = "andrew"
profileArray.append(p3)
self.profileTableView.reloadData()
}
}
解决方案
根据你的名字CustomProfileViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "customProfileCell", for: indexPath) as! CustomProfileViewCell
//
profileTableView.register(UINib(nibName: "CustomProfileViewCell",bundle: nil) , forCellReuseIdentifier: "customProfileCell")
推荐阅读
- javascript - JavaScript 对象解构中的变量声明错误和混淆
- javascript - Passport cookie 不适用于前端网站
- swift - 在swift中找不到什么是image.layer
- firebase - 如何使用开发变量通过 Github Actions 将 Gatsby 项目部署到 Firebase 托管?
- python - 如何使用正则表达式隔离美元金额?
- html - 为什么下面的 django 视图代码不起作用?
- bash - sed 或 awk 命令用 json 对象替换变量,如下所示
- ansible - Ansible win_chocolatey 说它安装了 Notepad++,但它没有?
- python - 使用 SQLAlchemy 1.4 经典/命令式映射风格?
- react-native - 未找到 React Native Gradle SDK 位置。折旧的 Gradle 功能