Search code examples
iosswiftuitableviewxib

Cant find Cell Identifier(No storyboard)


Hi I hav an issue where I cant seem to find the cell identifier to put into my table. I have 5 files.I am new to xcode but I need to code without a storyboard for my school project.

I am following the tutorial here -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

Error Message

enter image description here

Image of me adding register not sure whether i added correctly..

enter image description here


Solution

  • Open your .xib file and drag and drop UITableViewCell not UIView.

    enter image description here

    After that select attribute inspector you will see cell identifier textbook where you need to set cell identifier

    enter image description here

    Don't forget to register table view cell before using it!!

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

    or

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