每當我運行應用程序時,tableView
都沒有數據,等待用戶輸入。問題是,如果numberOfSections
是1,它工作得很好,但是當我將其更改爲2崩潰,因爲指數超出範圍UITableView部分索引超出範圍但它並不是真的越界
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "expenseCell") as? ExpenseCell else { return UITableViewCell() }
let budget = userBudget[indexPath.section][indexPath.row]
cell.delegate = self
cell.configureCell(budget: budget)
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.none
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userBudget[section].count // <- Crash here
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Practice with Sections \(section)"
}
什麼是userBudget數據arrray? – iPatel
'userBudget'中只有一個數組。所以首先'userBudget'的值,並確保它有多個數組。 –
更改您的numberOfSections以返回userBudget?.count ?? 0(如果userBudget是可選的)ow userBudget.count。不要硬編碼值。 –