【Swift】Cellの高さを変更する

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

 

Cellを高さを変えるには、

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        tableView.estimatedRowHeight = 20 //セルの高さ
        return UITableView.automaticDimension //自動設定
}

を使う方法があるけど、なぜか高さは変わらない。

(コードの位置を変えたり思考錯誤したがダメだった)

 

ただ単に高さを変えるなら、heightForRowAtをつかえばOK

 

完成図

 

f:id:nekokichi_yos2:20181213211550p:plain

(セルの高さは100)

 

ストーリーボード

 

f:id:nekokichi_yos2:20181213211633p:plain

 

ソースコード

 

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.delegate = self
        tableView.dataSource = self
        
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }

}