6
votes

I'm Working in Swift 4 and i'm facing problems in UITableViewCell. I need 5 different types of cells so I have make a .xib file and create 5 cells. But when register nib in TableViewController class with same xib and different Cell identifier.. its crashing:

Error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'invalid nib registered for identifier (IntakeFormTextFieldCell) - nib must contain exactly one top level object which must be a UITableViewCell instance

My code is:

override func viewDidLoad() {
    super.viewDidLoad()
    intakeFormTableView.register(UINib(nibName: "IntakeFormViews", bundle: nil), forCellReuseIdentifier: "IntakeFormTextFieldCell")
    intakeFormTableView.register(UINib(nibName: "IntakeFormViews", bundle: nil), forCellReuseIdentifier: "IntakeFormButtonCell")
    // Do any additional setup after loading the view.
}

My rough xib file is: enter image description here

My Question is how can I use different cells from one xib file? Is there any easy and good practice way for this?

4
Y you have to do that if cell content is the same ??? - Sh_Khan
No Cell content is different one have textfield one have button one have switch Button one have DatePicker like that... - Jon Striker
see my edit ........ - Sh_Khan
@MuhammadUsman No you can not one XIB as Multiple Cell Means in One Xib You can create multiple view but they will not consider as individual cell you have to create all 5 XIB for every cell and it is the best practice to keep code clean and in standard format. - Ravi Panchal

4 Answers

6
votes

You can't create multiple custom cells on One xib. You need to create 5 different Xib's for 5 different cells. It's also easy for you to understand the code flow.

override func viewDidLoad() {
   super.viewDidLoad()
   intakeFormTableView.register(UINib(nibName: "IntakeFormViews1", bundle: nil), forCellReuseIdentifier: "IntakeFormTextFieldCell1")
   intakeFormTableView.register(UINib(nibName: "IntakeFormViews2", bundle: nil), forCellReuseIdentifier: "IntakeFormButtonCell2")
   intakeFormTableView.register(UINib(nibName: "IntakeFormViews3", bundle: nil), forCellReuseIdentifier: "IntakeFormButtonCell3")
   intakeFormTableView.register(UINib(nibName: "IntakeFormViews4", bundle: nil), forCellReuseIdentifier: "IntakeFormButtonCell4")
   intakeFormTableView.register(UINib(nibName: "IntakeFormViews5", bundle: nil), forCellReuseIdentifier: "IntakeFormButtonCell5")
// Do any additional setup after loading the view.
}
0
votes

I think you have not given identifier "IntakeFormTextFieldCell" and "IntakeFormButtonCell" of your cells, so that your app get crash.

intakeFormTableView.register(UINib(nibName: "IntakeFormViews", bundle: nil), forCellReuseIdentifier: "IntakeFormTextFieldCell")
intakeFormTableView.register(UINib(nibName: "IntakeFormViews", bundle: nil), forCellReuseIdentifier: "IntakeFormButtonCell")

Each cell show have different xib with swift file and it should be override with UITableViewCell

0
votes

As crash says you must have only one top level object in the cell nib that is the cell view itself , so remove any dragged object out of the cell content

enter image description here

0
votes

Swift 5

 override func viewDidLoad() {
    super.viewDidLoad()

       // Register nib.
       self.tableView.register(UINib(nibName: "Nib1", bundle: nil), forCellReuseIdentifier: "Nib1_name_cell")
       self.tableView.register(UINib(nibName: "Nib2", bundle: nil), forCellReuseIdentifier: "Nib2_name_cell")
 }