【发布时间】:2018-03-27 19:49:58
【问题描述】:
每当用户在新部分中输入内容时,先前部分的 indexpath.row 都会被该部分的新行替换。我会给你一个例子来更好地理解我想说的话:
我有一个这样的表格视图:
第 1 节:
- 用户输入 #1
- 用户输入#2
现在用户创建了一个新部分。 (第 2 节)。当他为第 2 节输入一行时,tableview 变成了
第 1 节:
- 用户输入#3
- 用户输入#2
第 2 节:
- 用户输入#3
用户再次添加另一个输入:
第 1 节:
- 用户输入#3
- 用户输入#4
第 2 节:
- 用户输入#3
- 用户输入#4
所以前面部分的行被新行替换。我找到了所有这些的来源,但我不知道如何解决它。问题出在这行代码:
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.row]
let budget = userBudget[indexPath.section][indexPath.row]
但上面写着 Type 'Budget' has no subscript members
var userBudget : [预算] = []
而Budget 是一个CoreData 实体
【问题讨论】:
-
Budget有哪些字段?
标签: ios swift uitableview core-data