Xcode(Swift)错误 - 使用未声明的类型'CustomCell'

时间:2023-01-23 17:43:28

I've been learning table views from tutorials on YouTube. I was following every step told in the video, and did everything the same as the author of this tutorial, but for some reason I got a strange error - I've heard that it's about old version of Xcode used in the tutorial - I'm working on the latest one.

我一直在从YouTube上的教程中学习表格视图。我正在关注视频中讲述的每一步,并且做了与本教程的作者相同的一切,但由于某种原因我得到了一个奇怪的错误 - 我听说它是​​关于教程中使用的旧版Xcode - 我'我正在研究最新的一个。

Debugger tells that "CustomCell" is undeclared.

调试器告诉“CustomCell”未声明。

Every help will be appreciated!

每一个帮助将不胜感激!

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrayOfCwiczenia.count

}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomCell


    let person = arrayOfCwiczenia[indexPath.row]
cell.setCell(cwiczenia.nazwa, imageName: cwiczenia.obrazek)
    return cell


}

5 个解决方案

#1


1  

CustomCell looks to be a subclass of UITableViewCell

CustomCell看起来是UITableViewCell的子类

Looks like they left out the part where you need to create it

看起来他们遗漏了你需要创建它的部分

Create a new file called CustomCell and make sure it's base class is UITableViewCell

创建一个名为CustomCell的新文件,并确保它的基类是UITableViewCell

#2


0  

You must have a file where you define the behaviour of the custom cell - if that's not called 'CustomCell' then it won't be declared.

您必须有一个文件,您可以在其中定义自定义单元格的行为 - 如果该文件未被称为“CustomCell”,则不会声明它。

Just make sure that you have the same name when you define the class and when you use it.

在定义类和使用它时,只需确保您具有相同的名称。

#3


0  

I would suggest looking at Apple's walkthrough on how to implement a table view. It has step by step instructions with pictures. If you scroll down the the Design Custom Table Cells section I think you will be able to see how to link the custom cell to a file properly

我建议看看Apple关于如何实现表视图的演练。它有图片的分步说明。如果向下滚动“设计自定义表单元格”部分,我认为您将能够看到如何正确地将自定义单元格链接到文件

#4


0  

Your tutorial should have mentioned all the details, but here it goes...

你的教程应该已经提到了所有的细节,但在这里......

  1. You need to define a subclass of UITableViewCell named CustomCell:

    您需要定义名为CustomCell的UITableViewCell的子类:

    import UIKit class CustomCell: UITableViewCell { // Your custom properties/methods.outlets/etc. }

    import UIKit类CustomCell:UITableViewCell {//您的自定义属性/ methods.outlets / etc. }

(typically, in a source file named CustomCell.swift, but this is not compulsory)

(通常,在名为CustomCell.swift的源文件中,但这不是强制性的)

  1. If you are using a storyboard, you need to set the class of your prototype cells to "CustomCell" in the identity inspector (third tab from the left on the right inspector pane - the one with the icon that looks like a newspapaer):
  2. 如果您正在使用故事板,则需要在身份检查器中将原型单元的类设置为“CustomCell”(右侧检查器窗格左侧的第三个选项卡 - 具有看起来像newspapaer的图标的选项卡):

Xcode(Swift)错误 - 使用未声明的类型'CustomCell'

  1. Also, In the attributes inspector (fourth tab from the right, icon looks like a slider), set the cell's identifier (in the case of your code, "Cell"):
  2. 此外,在属性检查器(右侧的第四个选项卡,图标看起来像滑块)中,设置单元格的标识符(在代码的情况下,“单元格”):

Xcode(Swift)错误 - 使用未声明的类型'CustomCell'

  1. If you are not using a storyboard, you need instead to register the custom cell class and identifier programmatically:

    如果您不使用故事板,则需要以编程方式注册自定义单元类和标识符:

    func viewDidLoad()
    {
        tableView.registerClass(CustomCell.self, forCellReuseIdentifier: "Cell")
        // other setup...
    }
    

#5


0  

I had this same error. Tried cleaning and building which worked but the main issue seemed to just be saving the CustomCell which then becomes recognised by the compiler and removes the error.

我有同样的错误。尝试清理和构建工作,但主要问题似乎只是保存CustomCell然后被编译器识别并删除错误。

Its not limited to cells I've had it with other custom classes before as well. Good one to know about though!

它不仅限于我以前使用其他自定义类的单元格。不过要知道的好人!

#1


1  

CustomCell looks to be a subclass of UITableViewCell

CustomCell看起来是UITableViewCell的子类

Looks like they left out the part where you need to create it

看起来他们遗漏了你需要创建它的部分

Create a new file called CustomCell and make sure it's base class is UITableViewCell

创建一个名为CustomCell的新文件,并确保它的基类是UITableViewCell

#2


0  

You must have a file where you define the behaviour of the custom cell - if that's not called 'CustomCell' then it won't be declared.

您必须有一个文件,您可以在其中定义自定义单元格的行为 - 如果该文件未被称为“CustomCell”,则不会声明它。

Just make sure that you have the same name when you define the class and when you use it.

在定义类和使用它时,只需确保您具有相同的名称。

#3


0  

I would suggest looking at Apple's walkthrough on how to implement a table view. It has step by step instructions with pictures. If you scroll down the the Design Custom Table Cells section I think you will be able to see how to link the custom cell to a file properly

我建议看看Apple关于如何实现表视图的演练。它有图片的分步说明。如果向下滚动“设计自定义表单元格”部分,我认为您将能够看到如何正确地将自定义单元格链接到文件

#4


0  

Your tutorial should have mentioned all the details, but here it goes...

你的教程应该已经提到了所有的细节,但在这里......

  1. You need to define a subclass of UITableViewCell named CustomCell:

    您需要定义名为CustomCell的UITableViewCell的子类:

    import UIKit class CustomCell: UITableViewCell { // Your custom properties/methods.outlets/etc. }

    import UIKit类CustomCell:UITableViewCell {//您的自定义属性/ methods.outlets / etc. }

(typically, in a source file named CustomCell.swift, but this is not compulsory)

(通常,在名为CustomCell.swift的源文件中,但这不是强制性的)

  1. If you are using a storyboard, you need to set the class of your prototype cells to "CustomCell" in the identity inspector (third tab from the left on the right inspector pane - the one with the icon that looks like a newspapaer):
  2. 如果您正在使用故事板,则需要在身份检查器中将原型单元的类设置为“CustomCell”(右侧检查器窗格左侧的第三个选项卡 - 具有看起来像newspapaer的图标的选项卡):

Xcode(Swift)错误 - 使用未声明的类型'CustomCell'

  1. Also, In the attributes inspector (fourth tab from the right, icon looks like a slider), set the cell's identifier (in the case of your code, "Cell"):
  2. 此外,在属性检查器(右侧的第四个选项卡,图标看起来像滑块)中,设置单元格的标识符(在代码的情况下,“单元格”):

Xcode(Swift)错误 - 使用未声明的类型'CustomCell'

  1. If you are not using a storyboard, you need instead to register the custom cell class and identifier programmatically:

    如果您不使用故事板,则需要以编程方式注册自定义单元类和标识符:

    func viewDidLoad()
    {
        tableView.registerClass(CustomCell.self, forCellReuseIdentifier: "Cell")
        // other setup...
    }
    

#5


0  

I had this same error. Tried cleaning and building which worked but the main issue seemed to just be saving the CustomCell which then becomes recognised by the compiler and removes the error.

我有同样的错误。尝试清理和构建工作,但主要问题似乎只是保存CustomCell然后被编译器识别并删除错误。

Its not limited to cells I've had it with other custom classes before as well. Good one to know about though!

它不仅限于我以前使用其他自定义类的单元格。不过要知道的好人!