简体   繁体   English

如何获取indexPath.row和indexPath.section

[英]How do I get both indexPath.row and indexPath.section

Whenever the user inputs something in a new section, the indexpath.row for the previous sections gets replaced with the new row of the section. 每当用户在新部分中输入内容时,先前部分的indexpath.row就会被该部分的新行替换。 I will give you an example to understand better what I'm trying to say: 我会给你一个例子,以更好地理解我想说的话:

I have a tableview like this: 我有一个这样的表视图:

Section 1: 第1节:

  1. User input #1 用户输入#1
  2. User input #2 用户输入#2

Now the user creates a new Section. 现在,用户创建一个新的部分。 (Section 2). (第2节)。 When he inputs a row for the Section 2, the tableview turns into 当他为第2部分输入一行时,表格视图会变成

Section 1: 第1节:

  1. User input #3 用户输入#3
  2. User input #2 用户输入2

Section 2: 第2节:

  1. User input #3 用户输入#3

The user again adds another input: 用户再次添加另一个输入:

Section 1: 第1节:

  1. User input #3 用户输入#3
  2. User input #4 用户输入#4

Section 2: 第2节:

  1. User input #3 用户输入#3
  2. User input #4 用户输入#4

So the rows for the previous sections gets replaced by the new rows. 因此,前几节的行将被新行替换。 I found where all of this comes from but I don't know how to fix it. 我找到了所有这些来源,但我不知道如何解决。 The problems come from this line of code: 问题来自以下代码行:

    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
}

Because only the indexPath.row is given, not the indexPath.section. 因为仅给出indexPath.row,而不给出indexPath.section。 My question is, how do I add a cell for indexPath.section and indexPath.row ? 我的问题是,如何为indexPath.section和indexPath.row添加一个单元格?

I tried modifying let budget = userBudget[indexPath.row] with let budget = userBudget[indexPath.section][indexPath.row] but it says Type 'Budget' has no subscript members 我尝试用let budget = userBudget[indexPath.section][indexPath.row]修改let budget = userBudget[indexPath.row] ,但是它说Type 'Budget' has no subscript members

var userBudget : [Budget] = [] var userBudget:[预算] = []

and Budget is a CoreData Entity Budget是一个CoreData实体

The problem is in your array. 问题出在您的阵列中。 Your array must be nested array. 您的数组必须是嵌套数组。

For eg 例如

var userBudget: [[Budget]] = []

Change your cellForRowAt indexPath function like below: 如下更改您的cellForRowAt indexPath函数:

let budget = userBudget[indexPath.section][indexPath.row]
cell.delegate = self
cell.configureCell(budget: budget)

NOTE: Add "array of Budget" in userBudget instead of "Budget". 注意:在userBudget中添加“预算数组”,而不是“ Budget”。 "array of Budget" is describe as section. “预算的数组”被描述为一节。

Simple example of Array of Array of String: 字符串数组数组的简单示例:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM