2017-10-26 2 views
0

swift3에서 JSON 서비스 응답 배열 수를 기반으로 여러 동적 섹션을 만드는 방법으로 전체 동적 양식을 작성하고 있습니다. 각 섹션에는 다른 요소가 있습니다.swift3에서 동적 tableView 섹션을 만드는 방법은 무엇입니까?

예 : JSON 응답 배열 수 3 tableview 섹션을 구현하는 인덱스 섹션을 기반으로 각기 다른 요소가있는 각 인덱스입니다.

예 데이터 :

[ 
    [ 
    "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" 
    ] 
] 

답변

0

나는 당신이 요구하는 생각에 자상을 ... 당신과 같은 형태의 tableview (jQuery과)를 만드는 방법을 알고하는 거 싶어 해요 아래 ...하지만 반환되는 JSON에 따라 섹션 당 더 많은 또는 더 적은 수의 행을 가질 수 있어야합니다.

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 

먼저 JSON 물건을 배열 배열 ([]]에 배치해야합니다. 그것은 몇 가지 구문 오류가있을 수 있으므로

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

jQuery과 코드는 아래와 같이 보일 것 ...

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. 
} 

} 

는이 코드를 테스트하지 않았다. 이것은 가까운 당신을 얻을 것입니다.

관련 문제