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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30368747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:10:42  来源:igfitidea点击:

Swift - Impossible to use "registerNib" on my tableView to add a custom cell

xcodeuitableviewswiftcell

提问by Nicolas Meienberger

I'm just figuring out how to add custom cells to a tableView in Swift. I watched a lot of tutorials and they all say at some point to use something like tableView.registerNibwhich is not working for me !

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

This is the code I'm using in my tableViewController class :

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

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

When I try to build, I have an error on the second line which says :

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

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

无法使用类型为“(UINib, forCellReuseIdentifier: String)”的参数列表调用“registerNib”。

What can I do ? All the tutorials and other answers about custom cells are using this code.

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

Thanks in advance

提前致谢

回答by Adam Bardon

I'm using following code inside tableView:cellForRowAtIndexPathfunction:

我在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

If you have your custom cell in storyboard or .xib file, don't forget to set Identifier(in this case CustomCell) in Attributes inspector

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

回答by Obj-Swift

After some head-banging I realized my error:

经过一番猛烈撞击后,我意识到了我的错误:

Instead of "forCellReuseIdentifier" I was supposed to use forCellWithReuseIdentifier. With so many things changing with Swift so swiftly, Its hard to keep up with all the changes. Hope this helps.

我应该使用 forCellWithReuseIdentifier 而不是“forCellReuseIdentifier”。Swift 的变化如此之快,很难跟上所有的变化。希望这可以帮助。

Solution that worked for me:

对我有用的解决方案:

self.collectionView!.register(UINib(nibName: "CategoryCVCell", bundle: nil), forCellWithReuseIdentifier: "CategoryCVCell")

回答by Nicolas Meienberger

Ok... I don't know what was the problem, but removing the file reference of my cell.xibin my project and adding it again just solved the problem. I already had some problems resolved like that in the past.

好的...我不知道是什么问题,但是cell.xib在我的项目中删除我的文件引用并再次添加它只是解决了问题。过去我已经解决了一些问题。

Thank you all for your quick answers !

谢谢大家的快速回答!

回答by Hugo Jordao

In my case the solution was changing this:

在我的情况下,解决方案正在改变这一点:

collectionView.register(MovieCollectionViewCell.self, forCellWithReuseIdentifier: "moviesCollectionView")

To this:

对此:

collectionView.register(UINib(nibName: "MovieCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "moviesCollectionView")

回答by Heeba Khan

I've used a generic function , which I've called like this.

我使用了一个通用函数,我像这样调用它。

tableView.register(UINib(nibName: R.reuseIdentifier.uploadDocHeaderCell.identifier, bundle:nil), forCellReuseIdentifier: R.reuseIdentifier.uploadDocHeaderCell.identifier)

The generic class was used from github, you can just use the following function though:

泛型类是从 github 使用的,但您可以只使用以下函数:

public func register<Resource: NibResourceType & ReuseIdentifierType>(_ nibResource: Resource)
    where Resource.ReusableType: UICollectionViewCell
  {
    register(UINib(resource: nibResource), forCellWithReuseIdentifier: nibResource.identifier)
  }

The link for the GitHub resource is:

GitHub 资源的链接是:

Link attached for reference

附上链接供参考

回答by Haya Hashmat

In your viewDidLoad add following lines:

在您的 viewDidLoad 添加以下几行:

let nibName = UINib(nibName: "cell_name", bundle:nil)
        collectionView.register(nibName, forCellWithReuseIdentifier: "cell_name")

And then in your cellForItemAt method,add the following lines:

然后在您的 cellForItemAt 方法中,添加以下几行:

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell_name", for: indexPath)as! cell_name

Make sure to give the identifier to the cell.

确保为单元格提供标识符。