2017-10-17 11 views
0

アプリを実行するたびに、tableViewにはユーザーが入力を待つデータがありません。問題はnumberOfSectionsが1であれば、それだけで正常に動作しますが、私は2に変更したときに、私はそれが含まれているとしながら、あなたがuserBudgetindex 1にアクセスしているので、インデックスが範囲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)" 
} 
+1

func numberOfSections(in tableView: UITableView) -> Int { return 2 } 

を置き換えますか? – iPatel

+0

'userBudget'には1つの配列しかありません。したがって、まず 'userBudget'の値を確認し、それに複数の配列があることを確認してください。 –

+1

numberOfSectionsを変更してuserBudget?countを返します。 0(userBudgetがオプションの場合)ow userBudget.count。値をハードコードしないでください。 –

答えて

0

の外にそれがあるので、それがクラッシュするということです0 indexのみ。

+0

私はあなたが言うことを理解していません。 – AndreiVataselu

0

このクラッシュは、配列が原因で発生しますuserBudget、2番目の位置にアクセスしようとしている瞬間に2つの要素がありません。

あなたはuserBudgetがminimium上の2つの要素を持っていることを守る必要があります...

あなたはあなたのviewDidLoad上userBudgetに値を代入する必要があることをする必要があります。

+0

しかしこれはどのように可能ですか?私が言ったように、var ** userBudget **は1次元配列で、すべてうまくいきました。私はこれを2D配列に設定しました.1つのセクションしか使用しないので、* userBudget [0] [indexPath.row] *は1D配列と同じですが、これはクラッシュします。ユーザーがまだ何も入力していないので、ViewDidLoadに値を設定することはできません。 – AndreiVataselu

0

テーブルビューのデリゲートに2つのセクションがありますが、配列が1つしか含まれていないとします。基本的に、numberOfRowsInSection関数内のuserBadget [1]に到達しようとすると、存在しないためクラッシュします。

はuserBudgetのarrray内のデータは何

func numberOfSections(in tableView: UITableView) -> Int { 
    return userBudget.count 
} 
関連する問題