What is the difference between tableViewCell and xib/nib in swift ? And when to use what?

139 Views Asked by At

I am so confused that whatever we can do through .xib/nib same thing can be done through normal tableViewCell. So whats the difference between them and when to use what?

1

There are 1 best solutions below

0
OguzzYlcn On

You can create a TableViewCell using xib to design your custom cell and you can connect xib (design) with swift file of cell.

CustomButtonTableViewCell.swift

CustomButtonTableViewCell.xib

Using xib to create TableView Cells is the better way if your TableView has multiple custom cells.


You can create extension for TableView to register cells.

extension UITableView {

func registerCell(identifier:String) {
    self.register(UINib(nibName: identifier, bundle: nil), forCellReuseIdentifier: identifier)
    self.tableFooterView = UIView()
    self.rowHeight = UITableView.automaticDimension
    self.separatorStyle = .none
} }

then you can register all custom cells to TableView and use it in UIViewController class.

tableView.registerCell(identifier: identifierButton)
tableView.registerCell(identifier: identifierTF)
tableView.registerCell(identifier: identifierAttText)
tableView.registerCell(identifier: identifierImage)