CustomCell.swift
import UIKit
class CustomCell: UITableViewCell {
let titleLabel = UILabel()
var game: String? {
didSet {
guard let game = game else { return }
titleLabel.text = game
}
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setup()
layout()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension CustomCell {
func setup() {
titleLabel.translatesAutoresizingMaskIntoConstraints = false
}
func layout() {
// don't disable translatesAutoresizingMaskIntoConstraints on the cell itself
contentView.addSubview(titleLabel) // important!
NSLayoutConstraint.activate([
titleLabel.leadingAnchor.constraint(equalToSystemSpacingAfter: leadingAnchor, multiplier: 1),
titleLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
])
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
let games = [
"Pacman",
"Space Invaders",
"Space Patrol",
]
let cellId = "cellId"
var tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
func setupViews() {
tableView.delegate = self
tableView.dataSource = self
tableView.register(CustomCell.self, forCellReuseIdentifier: cellId)
tableView.tableFooterView = UIView()
view = tableView
}
}
extension ViewController: UITableViewDelegate {
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! CustomCell
cell.game = games[indexPath.row]
cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return games.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}
CustomCell.swift
import UIKit
class CustomCell: UITableViewCell {
@IBOutlet var firstNameLabel: UILabel!
@IBOutlet var lastNameLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet var myTableView: UITableView!
let firstNames = ["Peter", "Paul", "Mary"]
let lastNames = ["Smith", "Jones", "Johnson"]
override func viewDidLoad() {
super.viewDidLoad()
myTableView.delegate = self
myTableView.dataSource = self
myTableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "customCellIdentifier")
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return firstNames.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCellIdentifier", for: indexPath) as! CustomCell
cell.firstNameLabel.text = firstNames[indexPath.row]
cell.lastNameLabel.text = lastNames[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 77
}
}
- Create a new UITableViewCell class with xib
- Give it an identifier
- Design your cell
- Create your cell and add the IBOutlets
import UIKit
class CustomCell: UITableViewCell {
@IBOutlet var firstNameLabel: UILabel!
@IBOutlet var lastNameLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
}
- Load xib in ViewController
override func viewDidLoad() {
myTableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "customCellIdentifier")
}
- Load cell in cellForRowAt
let cell = tableView.dequeueReusableCell(withIdentifier: "customCellIdentifier", for: indexPath) as! CustomCell
- Set the height
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 77
}
Voila!