-1

when I implemented the first tableView and its working fine, however, I try to implement another tableView on the same viewController on Tabbar then the crash occurs.

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

enter image description here

Inder_iOS
  • 1,636
  • 1
  • 12
  • 20
  • could u show ur code what u have tried @iOS – Dilip Tiwari Mar 30 '18 at 05:11
  • @DilipTiwari, I am not doing anything, I just add the second tableView to ViewController that is in under tabbar and run the app. – Inder_iOS Mar 30 '18 at 05:14
  • when I using single tableView under tab bar then it's working – Inder_iOS Mar 30 '18 at 05:15
  • https://stackoverflow.com/questions/23526756/unable-to-dequeue-a-cell-with-identifier-cell-must-register-a-nib-or-a-class-f or please share demo code – Hitesh Surani Mar 30 '18 at 05:18
  • Show your storyboard’s screenshot with two table selected and attribute inspector must be visible in that. I think you are missing cell identifier. Another thing is using cell and missed to register cell class with table view. – Sagar Chauhan Mar 30 '18 at 05:20
  • @PaulMarshal, please check the screenshot, 1 identify tableView first and 2 identify tableView second – Inder_iOS Mar 30 '18 at 05:27

4 Answers4

3

As you are putting two table views in the same controller, the delegate/data source for both table views will connect to the same controller. You will have to identify that which table view has to be updated. You can do this by taking the outlets of both the table views and then applying a simple conditional construct in cellForRowAt.

This is how I implemented.

Controller embedded in tab bar, with two table views

Code

    extension ViewController: UITableViewDelegate, UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == firstTableView
        {
            if let cell = tableView.dequeueReusableCell(withIdentifier: "prototypeOne", for: indexPath) as? UITableViewCell
            {
                cell.textLabel?.text = "First table view. Row \(indexPath.row)"
                return cell
            }
            else
            {
                let cell = UITableViewCell.init(style: .default, reuseIdentifier: "prototypeOne")
                cell.textLabel?.text = "First table view. Row \(indexPath.row)"
                return cell
            }
        }
        else if tableView == secondTableView
        {
            if let cell = tableView.dequeueReusableCell(withIdentifier: "prototypeTwo", for: indexPath) as? UITableViewCell
            {
                cell.textLabel?.text = "Second table view. Row \(indexPath.row)"
                return cell
            }
            else
            {
                let cell = UITableViewCell.init(style: .default, reuseIdentifier: "prototypeTwo")
                cell.textLabel?.text = "Second table view. Row \(indexPath.row)"
                return cell
            }
        }
        return UITableViewCell()
    }
}

Output

enter image description here

Vinodh
  • 5,262
  • 4
  • 38
  • 68
Pallav Trivedi
  • 316
  • 1
  • 15
1

Choose your prototipe cell in the Storyboard and set the identifier as Cell in the Inspectors at the right. Note that this identifier is different to the cell class.

Alejandro Iván
  • 3,969
  • 1
  • 21
  • 30
  • Please noted I am using with Tabbar – Inder_iOS Mar 30 '18 at 05:35
  • That’s irrelevant. Your specific error says you’re trying to dequeue a cell with an identifier “Cell” but that identifier isn’t defined to any cell. If you add that identifier to the cell, your error should disappear. – Alejandro Iván Mar 30 '18 at 05:36
  • If you’re using two table views, add outlets to them and compare to the tableView parameter with an `if` statement. According to that, dequeue with one identifier or another. Maybe you only have defined the identifier on one tableView and when dequeuing on the other produces the error. – Alejandro Iván Mar 30 '18 at 05:40
0

Register same uitableviewcell in second uitableview.

Abhishek Singh
  • 172
  • 1
  • 1
  • 9
0

Compare your both table with tableview object :

create IBOutlet of both table suppose table1 and table2 then in any delegate/Datasource method of tableview compare tableview and execute code

Eg. : I have define numberOfRowsInSection for 2 tableview

  - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(tableView == self.table1)
{
return self.menuTitles.count;
}
else
{
return ModelCollection.count;
}
Prakash Tripathi
  • 137
  • 1
  • 13