TableViewsの使い方をマスターしようとしていますが、私はプログラムで入力しようとしています。これはうまくいっていますが、このプログラムを変更して、あらかじめ設定されたセクションではなく空のセクションを作成したいと思います。iOS TableViewで空のセクションを作成する
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var rowCount = 1
var sectionCount = 1
func numberOfSections(in tableView: UITableView) -> Int {
return sectionCount
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section \(section+1)"
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rowCount
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath)
cell.textLabel?.text = "Entry \(indexPath.row+1) in Section \(indexPath.section+1)"
return cell
}
// func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//
// }
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var onSect: UISwitch!
@IBOutlet weak var addToTable: UIButton!
@IBAction func insertToTable(_ sender: Any) {
insert()
}
func insert(){
if onSect.isOn{
sectionCount += 1
}else{
rowCount += 1
}
tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
私はreloadData()
は基本的にtableView機能に基づいてテーブルをリメイクすることを見ることができますが、私はまたreloadData()
通話中に行でそれを埋めることなく、セクションを作るためにどのような方法を考えることはできません。
はあなたが0行のセクションを作りたいん:ここで
はthrid 1が空であることと、すべての残りが一列のみを有する5つのセクションの例ですか? – MEnnabah
基本的にはい。 –
numberOfRowsInSectionで0を返さないのはなぜですか? – MEnnabah