gpt4 book ai didi

xcode - Swift - 无法在我的 tableView 上使用 "registerNib"添加自定义单元格

转载 作者:IT王子 更新时间:2023-10-29 05:49:22 25 4
gpt4 key购买 nike

我只是想知道如何在 Swift 中将自定义单元格添加到 tableView。我看了很多教程,他们在某些时候都说要使用像 tableView.registerNib 这样的东西,这对我不起作用!

这是我在 tableViewController 类中使用的代码:

var nib = UINib(nibName: "ViewExerciceCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "ExerciceCell")

当我尝试构建时,第二行出现错误:

Cannot invoke 'registerNib' with an argument list of type '(UINib, forCellReuseIdentifier: String)'.

我能做什么?所有关于自定义单元格的教程和其他答案都使用此代码。

提前致谢

最佳答案

我在 tableView:cellForRowAtIndexPath 函数中使用以下代码:

var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as UITableViewCell
if cell == nil {
tableView.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCell")
cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as UITableViewCell!
}

return cell

如果您在 Storyboard或 .xib 文件中有自定义单元格,请不要忘记在属性检查器中设置 Identifier(在本例中为 CustomCell)

关于xcode - Swift - 无法在我的 tableView 上使用 "registerNib"添加自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30368747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com