I am getting an error - nib must contain exactly one top level object which must be a UITableViewCell instance'"

17,392

Solution 1

If you are using xib of tableviewcell then register your xib like this in viewDidLoad.

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

If you are using custom class of tableViewcell then try this one,

let placeCell : PlaceTableViewCell = tableView.dequeueReusableCell(withIdentifier: "placeCell") as! PlaceTableViewCell

As I am seeing your code work is almost correct. Hope my answer helps you.

Solution 2

I have added Tap gestures from Interface builder (XIB).

Remove that will solve this issue.

You have to add gesture in xib by programatically way

Solution 3

In my case it was a view which was added outside the cell. It shouldn't have been there

enter image description here

Solution 4

Having multiple table view cells in a .xib file also causes this error. Moving the table view cells in to their own separate .xib files resolved my error.

nib must contain exactly one top level object which must be a UITableViewCell instance

Solution 5

Open your 'PlaceCollectionViewCell.xib' file. Make sure there's only one top level view (look at the side panel, not just the canvas, it may not be visible). Make sure your view has a class assigned that is subclass of UITableViewCell (not UICollectionViewCell, xib name looks suspicious to me), as well as Reuse Identifier.

only one view on top level

has class and reuse identifier

Share:
17,392
Rakesh Mohan
Author by

Rakesh Mohan

Updated on June 19, 2022

Comments

  • Rakesh Mohan
    Rakesh Mohan about 2 years

    I am using a custom cell in my tableView but when I run i get the error which I have mentioned in my question.

     self.districtTableView.register(UINib(nibName: "PlaceCollectionViewCell", bundle: nil), forCellReuseIdentifier: "placeCell")
    
     func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        return false
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        // TODO: Your app can do something when textField finishes editing
    
        print("The textField ended editing. Do something based on app requirements.")
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
         return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return districts.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCell(withIdentifier: "placeCell") as! PlaceTableViewCell
        // Set text from the data model
        cell.distLabel.text = districts[indexPath.row]
        cell.textLabel?.font = distTextField.font
        return cell
    

    How can I get rid of this error. I have used different methods for registering a cell in table view. But it does not works. please help