Skip to content

Instantly share code, notes, and snippets.

@shankartshinde
Last active May 8, 2018 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shankartshinde/34a1bb6c0195e320d56418ef74161ae9 to your computer and use it in GitHub Desktop.
Save shankartshinde/34a1bb6c0195e320d56418ef74161ae9 to your computer and use it in GitHub Desktop.
How to register a cell for UITableViewCell reuse
let cellIdentifier = "CellIdentifier"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)
}
OR
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DefaultCell")!
return cell
}
The other option is to use register() with an Interface Builder nib file, like this:
tableView.register(UINib(nibName: "yourNib", bundle: nil), forCellReuseIdentifier: "CellFromNib")
// Register table cell class from nib
let cellNib = UINib(nibName: "TableCellNib", bundle: bundle)
self.tableView.registerNib(cellNib, forCellReuseIdentifier: self.tableCellId)
@shankartshinde
Copy link
Author

// Register table cell class from nib
let cellNib = UINib(nibName: "TableCellNib", bundle: bundle)
self.tableView.registerNib(cellNib, forCellReuseIdentifier: self.tableCellId)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment