找不到Cell Identifier(没有故事板)

时间:2018-03-29 06:36:46

标签: ios swift uitableview xib

嗨我有一个问题,我似乎无法找到要放入我的表格的单元格标识符。我有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

enter image description here

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

enter image description here

错误消息

enter image description here

我添加注册表的图片不确定我是否正确添加..

enter image description here

4 个答案:

答案 0 :(得分:1)

enter image description here

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

打开您的.xib文件,然后拖放UITableViewCell而不是UIView

enter image description here

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

enter image description here

在使用之前忘记注册表格查看单元格!!

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

let nib = UINib(nibName: "YourNibFileName", bundle: nil)

tableView.register(nib, forCellReuseIdentifier: "cellIdentifier")

答案 3 :(得分:0)

  1. Xib
  2. 中设置单元格标识符
  3. UITableView注册 Xib viewDidLoad

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