nstableview 添加行作为标题并设置连续数字

nstableview add row as title and set consecutive number

我的这个表视图有两列(名字,名字) 我用数组中的数据填充表格视图。 现在我想知道,我可以添加一个 "normal" 行(人)和一个像瓷砖一样的行。

例如:

第 1 行:男性 第 2 行:最大 |穆斯特曼 第 3 行:彼得 |迪伦

为此,我尝试像这样填充数组:

Data(firstName: "male persons", secondName: "", typ: "titel")
Data(firstname: "Max", secondName: "Mustermann", typ: "person")
Data(firstname: "Peter", secondName: "Düllen", typ: "person")

它有效,但这是将行设置为标题的正确方法吗?

第二个问题: 每行应该在另一列中得到一个连续的数字。 此刻我意识到这是行号。但现在的问题是,标题行不应该得到一个连续的数字。

小例子(行尾是我想实现的数字):

Data(firstName: "male persons", secondName: "", typ: "titel") []
Data(firstname: "Max", secondName: "Mustermann", typ: "person") [1]
Data(firstname: "Peter", secondName: "Düllen", typ: "person") [2]

Data(firstName: "male persons", secondName: "", typ: "titel") []
Data(firstname: "Max", secondName: "Mustermann", typ: "person") [3]
Data(firstname: "Peter", secondName: "Düllen", typ: "person")[4]

我该如何解决这种情况? 我希望你能理解我的问题。

您可以根据需要填充 table 视图。您的解决方案将正常工作。只需确保 Data 不是您应用程序“模型”层的一部分。 (“模型”层不应该知道数据是如何显示给用户的,所以它不应该知道那些标题行。)

还有其他方法可以做到。例如,您可以有一个部分数组:

struct Person {
    let firstName: String
    let lastName: String
}

struct Section {
    let title: String
    let people: [Person]
}

let person1 = Person(firstName: "Max", lastName: "Mustermann")
let person2 = Person(firstName: "Peter", lastName: "Düllen")
let section1 = Section(title: "male persons", people: [person1, person2])

let person3 = Person(firstName: "Max", lastName: "Mustermann")
let person4 = Person(firstName: "Peter", lastName: "Düllen")
let section2 = Section(title: "male persons", people: [person3, person4])

var sections = [section1, section2]

// Now implement the table view data source and
// delegate methods to display the sections array.

在此解决方案中,Person 可以成为应用程序“模型”层的一部分。