![](/img/trans.png)
[英]Ambiguous use of subscript using [indexPath.section][indexPath.row]
[英]How do I get both indexPath.row and indexPath.section
每當用戶在新部分中輸入內容時,先前部分的indexpath.row就會被該部分的新行替換。 我會給你一個例子,以更好地理解我想說的話:
我有一個這樣的表視圖:
第1節:
現在,用戶創建一個新的部分。 (第2節)。 當他為第2部分輸入一行時,表格視圖會變成
第1節:
第2節:
用戶再次添加另一個輸入:
第1節:
第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.row] // <- This
cell.delegate = self
cell.configureCell(budget: budget)
return cell
}
因為僅給出indexPath.row,而不給出indexPath.section。 我的問題是,如何為indexPath.section和indexPath.row添加一個單元格?
我嘗試用let budget = userBudget[indexPath.section][indexPath.row]
修改let budget = userBudget[indexPath.row]
,但是它說Type 'Budget' has no subscript members
var userBudget:[預算] = []
和Budget
是一個CoreData實體
問題出在您的陣列中。 您的數組必須是嵌套數組。
例如
var userBudget: [[Budget]] = []
如下更改您的cellForRowAt indexPath函數:
let budget = userBudget[indexPath.section][indexPath.row]
cell.delegate = self
cell.configureCell(budget: budget)
注意:在userBudget中添加“預算數組”,而不是“ Budget”。 “預算的數組”被描述為一節。
字符串數組數組的簡單示例:
var userBudget: [[String]] = [
["Section 1 - User input #1","Section 1 - User input #2","Section 1 - User input #3"], //Section 1 Inputs
["Section 2 - User input #1","Section 2 - User input #2"], //Section 2 Inputs
["Section 3 - User input #1","Section 3 - User input #2"] //Section 3 Inputs
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.