2017-10-26 3 views
0

Ich arbeite an vollständigen dynamischen Formular in Swift3, wie mehrere dynamische Abschnitte basierend auf JSON-Service-Antwort-Array-Anzahl erstellen. Und jeder Abschnitt hat verschiedene Elemente.Wie erstellt man dynamische TableView-Abschnitte in swift3?

Zum Beispiel: JSON Antwortarray 3. zählen Jeder Index verschiedene Elemente, bezogen auf Indexabschnitt tableview Abschnitte zu implementieren.

Beispieldaten:

[ 
    [ 
    "action-note", 
    "actions-control", 
    confirm, 
    confirm 
], 
    [ 
    "select-conditional", 
    select, 
    select, 
    "action-note", 
    "actions-control", 
    confirm, 
    confirm, 
    confirm 
], 
    [ 
    "tab-control", 
    "action-note", 
    "error-text", 
    checkbox, 
    "actions-control" 
    ] 
] 

Antwort

0

Ich werde einen Stich an, was ich glaube, Sie fragen ... Sie wollen wissen, wie ein Tableview zu machen ... (UITableView), die wie etwas aussieht unten ... aber Sie können mehr oder weniger Abschnitte und mehr oder weniger Zeilen pro Abschnitt basierend auf Ihrem zurückgegebenen JSON haben.

Section 1 
    action-note 
    actions-control 
    confirm 
    confirm 

Section 2 
    select-conditional 
    select 
    select 
    action-note 
    actions-control 
    confirm 
    confirm 
    confirm 

Section 3 
    tab-control 
    action-note 
    error-text 
    checkbox 
    actions-control 

Zuerst müssen Sie Ihre JSON-Zeug in ein Array von Arrays ([[]]) platzieren.

'[[Section name1, row1, row2, row3, row...n], 
    [Section name2, row1, row2, row3, row...n], 
    [Section name3, row1, row2, row3, row...n]]' 

Die UITableView Code wie unten ...

class ViewController: UIViewController, UITableViewDelegate, 
UITableViewDataSource { 


let arrOfArrFromJSON= '[["Section 1", "action-note", "actions-control", "confirm", "confirm"], 
    ["Section 2", "select-conditional", row2, row3, row...n], 
    [Section name3, row1, row2, row3, row...n]]' //Blah blah you can fill the rest of the array out 
let cellReuseIdentifier = "cell" //This is your cell identifier that was defined on the storyboard. 

override func viewDidLoad() { 
    super.viewDidLoad() 

} 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
// #warning Incomplete implementation, return the number of sections 

    return arrOfArrFromJSON.count //Counting the number of arrays within your array. Or number of sections within your array. 

} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return arrOfArrFromJSON[section].count //For each section, it counts the number of rows. 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier) as UITableViewCell! //Your UITableView is called 'tableView' in this example. It could be whatever you defined it as in the storyboard. 

    cell.textLabel?.text = arrOfArrFromJSON[indexPath.section][indexPath.row+1] // This will name each row or cell label according to whats in your array of arrays. The +1 is to not start at your section name. 

    return cell 
} 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    print("You tapped cell number \(indexPath.row) within section number \(indexPath.section)")//When you click on a cell it will return what cell you pressed in what section. 
} 

} 

aussehen werde ich diesen Code nicht getestet, so dass es einige Syntaxfehler haben. Dies sollte dir aber nahe kommen.

Verwandte Themen