【Swift】空のセルを消す(非表示)
完成図
解説
・tableView.tableFooterView = UIView()
・tableViewのFooterを空のUIViewに設定することで、セルがなくなる仕組みなのだろう
・空のセルを消すといっても、separator(線)を消しただけなんだけどね
ソースコード
import UIKit class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { @IBOutlet weak var tableView: UITableView! //セルのテキストを格納 var array = ["AAA","BBB","CCC"] override func viewDidLoad() { super.viewDidLoad() //これを追加するだけ tableView.tableFooterView = UIView() } // MARK: - Table view data source func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return array.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) cell.textLabel?.text = array[indexPath.row] return cell } }