【问题标题】:Error while trying to add a custom cell into table view尝试将自定义单元格添加到表格视图中时出错
【发布时间】:2018-12-29 04:25:06
【问题描述】:

我正在尝试将自定义单元格添加到我的表格视图中

这是我为自定义 CustomProfileView.xib 文件配置的内容

Main.storyboard

我一直得到

使用未声明的类型“CustomProfileCell”

这是我的全部代码

//
//  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()


    }

}

【问题讨论】:

  • forCellReuseIdentifier 应该是“customProfileCell”,而不是“CustomProfileCell”
  • 在这条线上profileTableView.register(UINib(nibName: "ProfileCell",bundle: nil) , forCellReuseIdentifier: "customProfileCell") ?
  • 我试过了,我得到了同样的错误。
  • 您的标识符 customProfileCell,而不是 ProfileCell。 ProfileCell 是您的班级名称
  • 你的意思是nibName?我很困惑。

标签: ios xcode uitableview xcode9


【解决方案1】:

根据您的 swift 名称 CustomProfileViewCell

let cell = tableView.dequeueReusableCell(withIdentifier: "customProfileCell", for: indexPath) as! CustomProfileViewCell

//

profileTableView.register(UINib(nibName: "CustomProfileViewCell",bundle: nil) , forCellReuseIdentifier: "customProfileCell")

【讨论】:

  • 啊,我想你发现了我的错误。你能分享一下你是怎么发现的吗?请
  • 默认情况下创建类时,名称 CustomProfileViewCell.swift 意味着类名是 CustomProfileViewCell ,我还查看了您的图像项目层次结构您应该在注册和出列中使用相同的标识符
  • @kyo 我在回答中附加了注册和出列行
  • 请查看我对上述评论的最新截图。
  • 复制答案中的第一行并将其替换为 cellForRowAt ,复制第二行并替换 viewDidLoad 中的寄存器行
【解决方案2】:

请先阅读this,它解释了reuseIdentifier 的工作原理及其含义。

首先,您的 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

【讨论】:

  • 笔尖名称也是 CustomProfileViewCell 而不是 CustomProfileCell
  • 好的。谢谢你的帮助。我修复了你指出的问题。
  • 我收到了这个Cannot convert return expression of type 'CustomProfileCell' to return type 'UITableViewCell' 有关系吗?
  • 演员是 as! CustomProfileViewCell
  • 改成这个 let cell = tableView.dequeueReusableCell(withIdentifier: "CustomProfileCell", for: indexPath) as! CustomProfileViewCell
猜你喜欢
  • 2011-10-30
  • 1970-01-01
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-18
  • 1970-01-01
  • 2012-01-27
相关资源
最近更新 更多