配列の数に合わせて `numberOfRowsInSection` の設定をしたいのですが
クロージャの外は nil が入ってしまいます、、、なぜでしょうか?
またデータの表示ですが、viewDidLoad ではなくviewWillAppear に記述した方がよいのでしょうか?
どなたかご教授いただけますと幸いです。
class CategoryTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! // CategoryCollectionViewControllerから選択されたCell番号を受け取る変数 var getCell: Int! // 選択された小カテゴリの配列の長さ変数 var arrayLength: Int! // 選択された小カテゴリの配列 // var smallCategoryArray: [String!] override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self let nib = UINib(nibName: "CategoryTableViewCell", bundle: nil) tableView.register(nib, forCellReuseIdentifier: "SmallCategory") // テーブル行の高さをAutoLayoutで自動調整する tableView.rowHeight = UITableViewAutomaticDimension // データ取得 let ref = Database.database().reference().child(Const.ServicePath) ref.child("category").observeSingleEvent(of: .value, with: {(snapshot) in let categoryData = Category(snapshot: snapshot) let lunchArray = categoryData.lunch let dinnerArray = categoryData.dinner let outdoorArray = categoryData.outdoor // 選択されたジャンルによってセルの数を変える // getCellの中身が switch self.getCell { case 0: self.arrayLength = outdoorArray.count // DEBUG: Optional(3) となります print("DEBUG: (self.arrayLength)") //self.smallCategoryArray = outdoorArray case 2: self.arrayLength = lunchArray.count //self.smallCategoryArray = outdoorArray case 3: self.arrayLength = dinnerArray.count //self.smallCategoryArray = outdoorArray default: break } }) // DEBUG: nil となります print("DEBUG: (self.arrayLength)") } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) } // データの数(=セルの数)を返すメソッド func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // DEBUG: nil となります return arrayLength }