Swift 3 での UITableView.dequeueReusableCell の使い方
毎回忘れるのでメモとして書いた
dequeueReusableCell
のところで CustomCell
に変換する綺麗な書き方は今のところ調べてないのでコピペは危険です
class VideoViewController: UIViewController { let cellClasses:[AnyClass] = [CustomCell.self] let comments:[String] = ["ほげ", "ふが"] override func viewDidLoad() { for clz in cellClasses { commentTableView.register( UINib(nibName: String(describing: clz), bundle: nil), forCellReuseIdentifier: String(describing: clz)) } } } extension ViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: CustomCell.self)) as! CustomCell? else { fatalError("UITableView.dequeueReusableCell Error") } cell.textLabel.text = comments[indexPath.row] return cell } }
めんどくさいですね
UITableView.register
と UITableView.dequeueReusableCell
の作業