Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Register Multiple TableViewCells from a xib file?

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?

like image 434
Jon Striker Avatar asked Feb 01 '18 07:02

Jon Striker


1 Answers

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.
}
like image 105
Sarabjit Singh Avatar answered Oct 05 '22 14:10

Sarabjit Singh