Ios 尝试将自定义单元格添加到表视图时出错
我正在尝试将自定义单元格添加到我的tableview中 这是我为customCustomProfileView.xib文件配置的 Main.storyboard 我一直在 使用未声明的类型“CustomProfileCell” 这是我的全部代码Ios 尝试将自定义单元格添加到表视图时出错,ios,xcode,uitableview,xcode9,Ios,Xcode,Uitableview,Xcode9,我正在尝试将自定义单元格添加到我的tableview中 这是我为customCustomProfileView.xib文件配置的 Main.storyboard 我一直在 使用未声明的类型“CustomProfileCell” 这是我的全部代码 // // FirstViewController.swift // tabbed // // Created by Ben Heng on 7/18/18. // Copyright © 2018 LR Web Design. All ri
//
// 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()
}
}
根据您的swift输入名称
CustomProfileViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "customProfileCell", for: indexPath) as! CustomProfileViewCell
//
根据您的swift输入名称
CustomProfileViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "customProfileCell", for: indexPath) as! CustomProfileViewCell
//
请先阅读,它解释了重用标识符的工作原理及其含义
首先,您的nib名称是CustomProfileViewCell
,但您正在使用ProfileCell
名称注册nib
第二个错误是,您在情节提要中使用的是ProfileCell
标识符,它应该与您定义的标识符相同,即-customProfileCell
你的手机注册应该是这样的
profileTableView.register(UINib(nibName: "CustomProfileViewCell",bundle: nil) , forCellReuseIdentifier: "customProfileCell")
然后你想在这里使用它:
let cell = tableView.dequeueReusableCell(withIdentifier: "customProfileCell", for: indexPath) as! CustomProfileViewCell
请先阅读,它解释了重用标识符的工作原理及其含义
首先,您的nib名称是CustomProfileViewCell
,但您正在使用ProfileCell
名称注册nib
第二个错误是,您在情节提要中使用的是ProfileCell
标识符,它应该与您定义的标识符相同,即-customProfileCell
你的手机注册应该是这样的
profileTableView.register(UINib(nibName: "CustomProfileViewCell",bundle: nil) , forCellReuseIdentifier: "customProfileCell")
然后你想在这里使用它:
let cell = tableView.dequeueReusableCell(withIdentifier: "customProfileCell", for: indexPath) as! CustomProfileViewCell
forCellReuseIdentifier应该是“customProfileCell”,而不是这一行的“customProfileCell”
profileTableView.register(UINib(nibName:“ProfileCell”,bundle:nil),forCellReuseIdentifier:“customProfileCell”)
?我已经试过了,我得到了相同的错误。您的标识符是customProfileCell
,而不是ProfileCell
。ProfileCell是您的类名您的意思是nibName
?我很困惑。forCellReuseIdentifier应该是“customProfileCell”,而不是这一行的“customProfileCell”profileTableView.register(UINib(nibName:“ProfileCell”,bundle:nil),forCellReuseIdentifier:“customProfileCell”)
?我已经试过了,我得到了同样的错误。您的标识符是customProfileCell
,而不是ProfileCell
。ProfileCell是您的类名您的意思是nibName
?我很困惑啊我想你发现了我的错误。。你能告诉我你是怎么发现的吗?请在默认情况下创建类名CustomProfileViewCell.swift表示类名为CustomProfileViewCell时,我查看了图像项目层次结构,您还应该在寄存器和中使用相同的标识符dequeue@kyo我在回答中附上了注册和出列行。请查看我对上述评论的最新截图。复制回答中的第一行并在cellForRowAt中替换它,复制第二行并在ViewDidLoadah中替换注册行。我想你发现了我的错误。。你能告诉我你是怎么发现的吗?请在默认情况下创建类名CustomProfileViewCell.swift表示类名为CustomProfileViewCell时,我查看了图像项目层次结构,您还应该在寄存器和中使用相同的标识符dequeue@kyo我在回答中附上了登记和出列行。请查看我对上述评论的最新截图。复制回答中的第一行,并将其替换到cellForRowAt,,复制第二个并替换viewDidLoadnib中的寄存器行名称也是CustomProfileViewCell而不是CustomProfileCelk。谢谢你的帮助。我修复了您指出的一个问题。我发现无法将“CustomProfileCell”类型的返回表达式转换为返回类型“UITableViewCell”
是否相关?强制转换为as!CustomProfileViewCell将其更改为let cell=tableView.dequeueReusableCell(标识符为:“CustomProfileCell”,for:indexPath)as!CustomProfileViewCellnib名称也是CustomProfileViewCell而不是CustomProfileCellok。谢谢你的帮助。我修复了您指出的一个问题。我发现无法将“CustomProfileCell”类型的返回表达式转换为返回类型“UITableViewCell”
是否相关?强制转换为as!CustomProfileViewCell将其更改为let cell=tableView.dequeueReusableCell(标识符为:“CustomProfileCell”,for:indexPath)as!CustomProfileViewCell