2017-06-14 53 views
1

我有這個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] 

我怎樣才能解決這種情況呢? 我希望你能理解我的問題。

回答

0

你可以像你想要的那樣填充表格視圖。您的解決方案將正常工作。只要確保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可以是應用程序的「模型」層的一部分。