1

here's the code:

import UIKit

class SliderMenuTableViewController: UITableViewController {

var tableArray = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()

    let age: String
    age = String(LoginViewController.AGE)

    let Profile = LoginViewController.NAME! + ", " + age

    tableArray = [Profile,"Second","Third","Fourth","Logout"]

}

// table view delegate method
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier(tableArray[indexPath.row], forIndexPath: indexPath) as UITableViewCell


    cell.textLabel?.text = tableArray[indexPath.row]
    if cell.textLabel?.text == tableArray[0] {

        cell.imageView?.frame = CGRectMake(10, 10, 50, 50)

        cell.imageView!.layer.borderWidth = 1
        cell.imageView!.layer.masksToBounds = false
        cell.imageView!.layer.borderColor = UIColor.blackColor().CGColor
        cell.imageView!.layer.cornerRadius = cell.imageView!.frame.width/2
        cell.imageView!.clipsToBounds = true
        cell.imageView?.image = LoginViewController.AVATAR
    }


    return cell
}

Error occured:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Viktor, Optional(29) - must register a nib or a class for the identifier or connect a prototype cell in a storyboard

I understand that I have to identifier the cell, how to do this from code cause the variable 'Profile' that in my tableArray isn't static and I can't identifier it from storyboard?

Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75
Denis Windover
  • 445
  • 2
  • 6
  • 20
  • In Interface Builder drag a table view cell into the table view. Then you can add the identifier. – vadian Sep 03 '16 at 09:20

3 Answers3

2

You did not give proper cell identifier in cell, because of that it's crashing. CellIdentifier you need to provide here in method which is you going to use for cell. Put the same identifier for same cell in storyboard.

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

    //configure your cell

    return cell
}

you have to register the cell in following ways depending upon how are you using custom cell

Register Custom cell

tableView.registerClass(MyCell.classForCoder(), forCellReuseIdentifier: kCellIdentifier)

Register Custom cell with Xib:

tableView.registerNib(UINib(nibName: "UICustomTableViewCell", bundle: nil), forCellReuseIdentifier: "UICustomTableViewCell")

Updated: Even you can assign the cell identifier in XIB as well if you are creating cell through XIB.

Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75
  • It's much more convenient - especially for beginners - to use prototype cells in Interface Builder than registering cells in code. – vadian Sep 03 '16 at 09:29
  • is this answer needs down vote? I guess my answer motivate to implement in another way? want to know reason for down vote. – Sanoj Kashyap Sep 03 '16 at 15:46
  • This is a much better answer. Aside from the fact that you didn't include the fact that you can set the identifier in Interface Builder. – Fogmeister Sep 03 '16 at 21:52
  • Don't you think mentioned line " Put the same identifier for same cell in storyboard." will saying same thing in a way that as it can assign in storyboard, in that same way you can achieve for other way. We are here solve the problem but we can motivate people to think beyond something. – Sanoj Kashyap Sep 04 '16 at 05:10
0

enter image description here

here's storyboard. first i'm using SWRevealViewController as slider menu. now by clicking on cell in the menu like you tell me it moves to another view controller but the menu doesn't work there.

and i used this code:

let mainStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) let vc : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Profile") as UIViewController self.presentViewController(vc, animated: true, completion: nil)

this:

    let vc: UIViewController =  (self.storyboard?.instantiateViewControllerWithIdentifier("viewContName"))!

self.navigationController?.pushViewController(vc, animated: true)

doesn't work. how to move to the profile view controller and menu still working?

Denis Windover
  • 445
  • 2
  • 6
  • 20
  • please edit your ques dont post ques as ans here we will help you sure .. – vaibhav Sep 03 '16 at 12:05
  • 1
    A general statement for understanding UI design: If you are using one storyboard **and** segues you **don't need** `instantiateViewControllerWithIdentifier` or `registerClass/registerNib` **at all**. You **do need** them only if you want to load UI from other nibs or storyboards or use multiple storyboards or create cells and controllers in code. – vadian Sep 03 '16 at 12:10
-1

This is because of your cell in not initilized properly with identifier. Use below code to create cell and use. Dynamic cell id initialization makes prob here.

let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

Instead of yours:

var cell = tableView.dequeueReusableCellWithIdentifier(tableArray[indexPath.row], forIndexPath: indexPath) as UITableViewCell

Edit:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // here you get the each cell by indexpath.row 
    // code here to go to the another viewController accordingly 
    print("You selected cell #\(indexPath.row)!")
}

Edit 2: Navigate to another view using below code on click over cell and show your customized data accordingly.

let vc: UIViewController =  (self.storyboard?.instantiateViewControllerWithIdentifier("viewContName"))!
self.navigationController?.pushViewController(vc, animated: true)
vaibhav
  • 4,038
  • 1
  • 21
  • 51
  • ok, its wokring but... now i need by clicking on each item of cell to move to another viewcontroller. before that i had 5 cells with another identifiers and i just set segue from storyboard to move to viewcontroller that i want. but now how do i do it? – Denis Windover Sep 03 '16 at 09:03
  • That dequeue method should not be used. You should use the one with the index path and register a class or nib properly. It's even in the docs. Don't revommend discouraged code. – Fogmeister Sep 03 '16 at 09:15
  • Basically the method ’dequeueReusableCellWithIdentifier: forIndexPath:` is perfectly fine. Only the *dynamic* identifier causes the problem. – vadian Sep 03 '16 at 09:17
  • so that's i suggested the same about dynamic identifier i dont know why down voted my ans .. – vaibhav Sep 03 '16 at 09:21
  • one more thing. which segue to choose in storyboard? i cant make from cell several segues 'Show' in Selection Segue to several viewcontrollers. what i supose to do? – Denis Windover Sep 03 '16 at 09:41
  • @vaibhav you edited your answer AFTER my down vote. You have edited your answer but still your first recommendation is to use a method that is discouraged. – Fogmeister Sep 03 '16 at 21:50
  • @Fogmeister it will be grateful to update my ans for right direction :) .. – vaibhav Sep 05 '16 at 12:24