nstableview添加行作为标题并设置连续编号
问题描述:
我有这个tableview有两列(名字,第二个名字) 我填充tableview与数组中的数据。 现在我想了解,我可以添加一个“正常”行(人)和一个像瓷砖一样的行。nstableview添加行作为标题并设置连续编号
例如:
行1:男性人 行2:最大| Mustermann 第3排:Peter | Düllen
对于我试图填补我的数组是这样的:
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]
我怎样才能解决这种情况呢? 我希望你能理解我的问题。
答
你可以像你想要的那样填充表格视图。您的解决方案将正常工作。只要确保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
可以是应用程序的“模型”层的一部分。