LoginSignup
5
2

More than 5 years have passed since last update.

Swift 3 での UITableView.dequeueReusableCell の使い方

Last updated at Posted at 2017-01-20

毎回忘れるのでメモとして書いた
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.registerUITableView.dequeueReusableCell の作業

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2