Ios 如何访问在xib中创建的自定义单元格
我在xib中设计了一个定制单元。并为此创建了一个类。该类的代码如下所示-Ios 如何访问在xib中创建的自定义单元格,ios,iphone,swift,uitableview,Ios,Iphone,Swift,Uitableview,我在xib中设计了一个定制单元。并为此创建了一个类。该类的代码如下所示- class ProjectsCell : UITableViewCell { @IBOutlet var projectNameLabel: UILabel! //This is outlet to which I will assign value. override func awakeFromNib() { super.awakeFromNib() // Initialization cod
class ProjectsCell : UITableViewCell {
@IBOutlet var projectNameLabel: UILabel! //This is outlet to which I will assign value.
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
现在我有了一个视图控制器,我正在尝试访问这个单元格。在故事板中,我给出了可重用标识符“Cell”。现在我像下面的代码一样使用这个单元格-
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as ProjectsCell
let project = projects[indexPath.row]
cell.projectNameLabel?.text = project.ProjectName //********* Here I am getting exception for projectNameLabel.
return cell
var array = NSBundle.mainBundle().loadNibNamed("ProjectsCell", owner: self, options: nil)
var cell = array[0] as ProjectsCell
let project = projects[indexPath.row]
cell.nameLabel?.text = project.Name
return cell
我认为那个标签是空的。我也尝试过以下方法,但也不起作用
var cell: ProjectsCell = tableView.dequeueReusableCellWithIdentifier("Cell") as ProjectsCell
tableView.registerNib(UINib(nibName: "ProjectsCell", bundle: nil), forCellReuseIdentifier: "Cell")
cell = tableView.dequeueReusableCellWithIdentifier("Cell") as ProjectsCell
如果有人遇到过同样的问题,会出现什么问题。您的自定义单元格应该继承UITableViewCell类。所以这个班级看起来是这样的
class ProjectsCell: UITableViewCell {
@IBOutlet var projectNameLabel: UILabel! //This is outlet to which I will assign value.
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
如果你有这样的手机,它应该能用。因为我在我的应用程序中就是这样做的。将来如果你创建一个类,你可以使用“文件->新文件…”菜单。在这里,您可以选择coca touch类并指定要从中继承的类,xcode将添加所有必要的函数。您已将自定义单元格排在队列中,但未使用以下方法对其进行初始化。 除此之外,您还必须将自定义类设置为UITableViewCell的子类,这是因为您将获得单元格的空值
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStylePlain reuseIdentifier: "Cell")
}
可能它设置了数据源和委托:
(来源:)我使用以下代码解决了这个问题-
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as ProjectsCell
let project = projects[indexPath.row]
cell.projectNameLabel?.text = project.ProjectName //********* Here I am getting exception for projectNameLabel.
return cell
var array = NSBundle.mainBundle().loadNibNamed("ProjectsCell", owner: self, options: nil)
var cell = array[0] as ProjectsCell
let project = projects[indexPath.row]
cell.nameLabel?.text = project.Name
return cell
感谢大家的贡献。:) 将代码注册单元格放在viewDidLoad方法中。目前我没有为此做任何事情。它不应该像
类项目吗?调用:UITableViewCell
?是否连接了.nib和.h?是的。。我忘了在问题里打那个。我已经做了。。但是什么也没有发生。另外,nib需要在viewDidLoad方法中注册(除了cellForRow方法之外的任何地方)。