0

I recently started programming using swift(swift newbie! :b) got an error and have no idea how to fix it :c

this is the code of viewcontroller.swift !

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITableViewDataSource,UITableViewDelegate {

@IBOutlet weak var picker1: UIPickerView!
@IBOutlet weak var keySelect: UITableView!

var Array = ["2", "3", "4"]

@IBOutlet weak var picker1label: UILabel!

@IBOutlet weak var keyselectView: UILabel!
@IBOutlet weak var keylabel: UIButton!

let intervalCellIdentifier = "intervalCellIdentifier"

var intervalNames = ["Q", "W", "E", "R", "T", "Y"]

let limit = 5

var answer1 = 0

override func viewDidLoad() {
    super.viewDidLoad()

    picker1.delegate = self
    picker1.dataSource = self

    //edited
    keySelect.delegate = self
    keySelect.dataSource = self

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}




func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {

    return Array[row]

}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return Array.count
}


func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

@IBAction func submit1(sender: AnyObject) {

    if(answer1 == 0) {
        picker1label.text = "2"
    }

    else if(answer1 == 1) {
        picker1label.text = "3"
    }

    else {
        picker1label.text = "4"
    }

}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    answer1 = row
}

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


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(intervalCellIdentifier,forIndexPath: indexPath) as UITableViewCell

    cell.accessoryType = .None
    cell.textLabel?.text = intervalNames[indexPath.row]

    return cell
}



//MARK: - UITableViewDelegate


func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {

    if let sr = tableView.indexPathsForSelectedRows {
        if sr.count == limit {
            let alertController = UIAlertController(title: "Oops", message:
                    "You are limited to \(limit) selections", preferredStyle: .Alert)
            alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: {action in
                }))
            self.presentViewController(alertController, animated: true, completion: nil)

            return nil
        }
    }

    return indexPath
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    print("selected  \(intervalNames[indexPath.row])")

    if let cell = tableView.cellForRowAtIndexPath(indexPath) {
        if cell.selected {
            cell.accessoryType = .Checkmark
        }
    }

    if let sr = tableView.indexPathsForSelectedRows {
        print("didDeselectRowAtIndexPath selected rows:\(sr)")
    }
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

    print("deselected  \(intervalNames[indexPath.row])")

    if let cell = tableView.cellForRowAtIndexPath(indexPath) {
        cell.accessoryType = .None
    }

    if let sr = tableView.indexPathsForSelectedRows {
        print("didDeselectRowAtIndexPath selected rows:\(sr)")
    }
}

}

2016-07-28 23:08:29.868 customkeyboard[60865:1611607] * Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.29.5/UITableView.m:6547 2016-07-28 23:08:29.945 customkeyboard[60865:1611607] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier intervalCellIdentifier - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

this is error explanation(?)

help me :3

Nirav D
  • 71,513
  • 12
  • 161
  • 183
Yumin Hwang
  • 159
  • 2
  • 2
  • 14
  • Are you using prototype cell or separate xib? – Nirav D Jul 28 '16 at 14:26
  • Possible duplicate of [Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:](http://stackoverflow.com/questions/12737860/assertion-failure-in-dequeuereusablecellwithidentifierforindexpath) – benuuu Jul 28 '16 at 14:30
  • thank you for all coments ! fixed it and forgot to reply ;-; <3 – Yumin Hwang Aug 22 '16 at 07:38

3 Answers3

2

1. With XIB file

You have to register the cell to table first, may be in viewDidLoad, as:

let nib = UINib(nibName: "CustomCell", bundle: NSBundle.mainBundle()) 
tableView.registerNib(nib, forCellReuseIdentifier: "CustomCellIdentifier")

This is applicable if we are using a custom cell with .xib file.

2. Without XIB file

And here is a solution if we are not creating separate .xib file for custom cell:

We need to create dynamic prototype for cell in table view as: enter image description here

Then we need to provide class name and reuse identifier for our custom cell as: enter image description here

enter image description here Here, no need to register class or nib!


Here is the cellForRowAtIndexPath implementation:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // Configure the cell...
    let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCellId", forIndexPath: indexPath) as! CustomTableViewCell
    cell.setCellIndexLabel(indexPath.row)

    return cell
}

This implementation will be same for both the above solutions.


3. Only Class Implementation
Without XIB, without Dynamic Prototype

Here is the custom cell without any XIB or Dynamic Prototype:

class WithoutNibCustomTableViewCell: UITableViewCell {

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    //do custom initialisation here, if any
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

class func identifier() -> String {
    return "WithoutNibCustomTableViewCellId"
}

//MARK: Public Methods
func setCellIndexLabel(index: Int) {
    let customLbl = UILabel(frame: CGRectMake(0, 0, 100, 40))
    customLbl.text = String(index)
    contentView.addSubview(customLbl)
}

}

Then, in table view, we need to register cell class as:

tableView!.registerClass(WithoutNibCustomTableViewCell.classForCoder(), forCellReuseIdentifier: WithoutNibCustomTableViewCell.identifier())

cellForRowAtIndexPath also same as above, like:

let cell = tableView.dequeueReusableCellWithIdentifier(WithoutNibCustomTableViewCell.identifier(), forIndexPath: indexPath) as! WithoutNibCustomTableViewCell
    cell.setCellIndexLabel(indexPath.row)
    return cell
D4ttatraya
  • 3,344
  • 1
  • 28
  • 50
0

in cellForRow change this : let cell = tableView.dequeueReusableCellWithIdentifier(intervalCellIdentifier,forIndexPath: indexPath) as UITableViewCell by let cell = tableView.dequeueReusableCellWithIdentifier(intervalCellIdentifier,forIndexPath: indexPath) as your cell class name .

Aymen BRomdhane
  • 143
  • 1
  • 14
0

I published a pod that will make your life easier while working with cells:

https://cocoapods.org/pods/CellRegistration

It simplifies the API to just one-line:

let : MyCustomCell = tableView.dequeueReusableCell(forIndexPath: indexPath)
Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71