【问题标题】:nstableview add row as title and set consecutive numbernstableview 添加行作为标题并设置连续编号
【发布时间】:2017-06-16 07:29:15
【问题描述】:

我有这个包含两列的表格视图(名字,第二个名字) 我用数组中的数据填充表格视图。 现在我想实现,我可以添加一个“普通”行(人)和一个像瓷砖一样的行。

例如:

第 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]

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

【问题讨论】:

    标签: arrays macos cocoa swift3 nstableview


    【解决方案1】:

    您可以随意填充表格视图。您的解决方案将正常工作。只需确保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 可以是应用“模型”层的一部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 2022-07-30
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多