Tag: UIKit/テーブル/セルの再利用
override func viewDidLoad() { super.viewDidLoad() self.tableView.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0) self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myCell") } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { // 素直にCellのインスタンスを生成することもできる。 // let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell") let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) cell.textLabel?.text = rows[indexPath.row] return cell }
※ストーリーボードで設定したプロトタイプセルを利用する場合、registerClassを呼び出してはいけない。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("myCell") if (cell == nil) { cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "myCell") } let theCell = cell! theCell.textLabel?.text = rows[indexPath.row] return theCell }