【问题标题】:Cant find Cell Identifier(No storyboard)找不到单元标识符(无情节提要)
【发布时间】:2018-03-29 06:36:46
【问题描述】:

您好,我遇到了一个问题,我似乎无法找到要放入表格的单元格标识符。我有 5 个文件。我是 xcode 的新手,但我需要在没有故事板的情况下为我的学校项目编写代码。

我正在学习这里的教程 -https://www.youtube.com/watch?v=kYmZ-4l0Yy4

ActiveCasesController.swift

//
//  ActiveCasesController.swift
//
//  Created by fypj on 29/3/18.
//  Copyright © 2018 fypj. All rights reserved.
//

import UIKit

class ActiveCasesController:UIViewController, UITableViewDelegate, UITableViewDataSource {

let elements = ["horse", "cat", "dog", "potato","horse", "cat", "dog", "potato","horse", "cat", "dog", "potato"]


var acView = ActiveCasesView()

override func viewDidLoad() {
    super.viewDidLoad()

    setupView()
    acView.tableView.delegate = self
    acView.tableView.dataSource = self
}

func setupView() {
    let mainView = ActiveCasesView(frame: self.view.frame)
    self.acView = mainView
    self.view.addSubview(acView)
    acView.setAnchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0)
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return elements.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:ActiveCaseCellView = tableView.dequeueReusableCell(withIdentifier: "customCell") as! ActiveCaseCellView

    cell.cellView.layer.cornerRadius = cell.cellView.frame.height / 2

    cell.lblCell.text = elements[indexPath.row]
    return cell
}


}

ActiveCasesView.swift

import UIKit

class ActiveCasesView: UIView{

@IBOutlet var mainView: UIView!
@IBOutlet weak var tableView: UITableView!

override init(frame: CGRect) {
    super.init(frame: frame)
    commonInit()
    //scrollView()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    commonInit()
}

private func commonInit(){
    Bundle.main.loadNibNamed("ACView", owner: self, options: nil)
    addSubview(mainView)
    mainView.frame = self.bounds
    mainView.autoresizingMask = [.flexibleHeight,.flexibleWidth]
}
}

ACView.xib

ActiveCaseCellView.swift

import UIKit

class ActiveCaseCellView:UITableViewCell {


@IBOutlet weak var cellView: UIView!
@IBOutlet weak var lblCell: UILabel!
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}
}

ACViewCell.xib

错误信息

我添加寄存器的图像不确定我是否添加正确..

【问题讨论】:

  • 原来我也需要将出口从文件所有者更改为自定义单元格

标签: ios swift uitableview xib


【解决方案1】:

use the cell identifier by selecting you cell in storyboard.
it must be same in viewcontroller and storyboard tableview cell.

【讨论】:

    【解决方案2】:

    打开您的.xib 文件并拖放UITableViewCell 而不是UIView

    在选择属性检查器之后,您将看到需要设置单元标识符的单元标识符教科书

    使用前别忘了注册表格视图单元格!!

    self.tableView.registerClass(<YourCustomCellClass>, forCellReuseIdentifier: "cellIdentifier")
    

    let nib = UINib(nibName: "YourNibFileName", bundle: nil)
    
    tableView.register(nib, forCellReuseIdentifier: "cellIdentifier")
    

    【讨论】:

    • 感谢您的帮助,但我仍然无法运行我已添加 let nib = UINib(nibName: "ACViewCell", bundle: nil) acView.tableView.register(nib, forCellReuseIdentifier: "customCell" ) 但我现在收到错误“他的类不符合键 cellView 的键值编码”
    • 你注册了cell吗?
    • 是的,我在视图中注册了单元格并加载了,但是当我启动它时出现错误“此类不符合关键 cellView 的键值编码”
    • 您的 ACView 不是 UITableViewCell....ActiveCaseCellView 是一个表格视图单元格...这就是您崩溃的原因。
    • 我已经改变了我的愚蠢错误......但现在我得到“无法在捆绑中加载 NIB”......
    【解决方案3】:

    您需要在您的 tableView 上调用 register(_:forCellReuseIdentifier:) 以使其在调用 dequeue... 时找到所需的单元格。 您可以在定义单元格的 xib 或情节提要中设置重用标识符。

    【讨论】:

      【解决方案4】:
      1. Xib中设置单元格标识符
      2. viewDidLoad 中使用UITableView 注册Xib

         tableView.register(UINib(nibName: "ActiveCaseCellView", bundle: Bundle.main), forCellReuseIdentifier: "customCell")   
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-02
        • 2014-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多