【Swift】カスタムセルの作り方

どうも、猫吉(@nekokichi1_yos2)です。

 

完成図

 

f:id:nekokichi_yos2:20181213213130p:plain

 

ストーリーボード

 「カスタムセル」

f:id:nekokichi_yos2:20181213213144p:plain

 

「TableView」

f:id:nekokichi_yos2:20181213213157p:plain

 

ソースコード

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.delegate = self
        tableView.dataSource = self
        
        //セルの高さを設定
        tableView.estimatedRowHeight = 80
        tableView.rowHeight = 80
        
        //カスタムセルをtableViewに登録
        tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "customcell")
        
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "customcell", for: indexPath)
        return cell
    }
    
    //こっちだけでも高さは設定可
//   func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//       return 80
//   }
    
}