2017-03-08 44 views
0

你好是新來的,所以我有這種類型的多維數組如何創建多節的tableView

[["name1","name2"],["name3,"name4"],["name5","name6"]]

的,我想創建多個部分的UITableView的

let tableView: UITableView = { 
    let table = UITableView() 

    return table 
}() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    view.backgroundColor = .white 
    tableView.delegate = self 
    tableView.dataSource = self 
    tableView.register(TableCell.self, forCellReuseIdentifier: "tablecell") 
    view.addSubview(tableView) 
    view.addConstraintsWithFormat("H:|[v0]|", views: tableView) 
    view.addConstraintsWithFormat("V:|[v0]|", views: tableView) 


} 
override func viewDidAppear(_ animated: Bool) { 
    print("Groups are \(allGroups.count)") 
    print(allGroups[0].count) 
} 
func numberOfSections(in tableView: UITableView) -> Int { 
    return allGroups.count 
} 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return allGroups[section].count 
} 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "tablecell") as! TableCell 
    for i in allGroups{ 
     cell.textLabel?.text = i[indexPath.row] 
     for j in i{ 
      print(j) 
     } 
    } 
    return cell 
} 
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return "Section \(section)" 
} 
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let vw = UIView() 
    vw.backgroundColor = UIColor.gray 

    return vw 
} 

} 

這裏是什麼我的代碼目前看起來像但每個部分都出現相同的數據

+3

你有什麼迄今所做?告訴我們你的代碼:-) –

+0

我堅持我不知道如何訪問數組用於我的代表,就像我不能計數'numberOfRowsInSection'或'numberOfSections' –

+1

@Johnmayowa用代碼編輯你的問題,你目前正在嘗試, –

回答

1
func numberOfSections(in tableView: UITableView) -> Int { 
    return allGroups.count 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return allGroups[section].count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "tablecell") as! TableCell 

    cell.textLabel?.text = allGroups[indexPath.section][indexPath.row] 
    return cell 
} 

備註

您不應該這樣做。

print(allGroups[0].count) 

也許allGroups沒有任何成員,所以allGroups [0]使超出範圍的問題崩潰。

+0

如何從我使用的數組中獲取部分的數量? –

+0

看看你的例子數據。 '[[「name1」,「name2」],[「name3,」name4「],[」name5「,」name6「]] 您認爲有多少部分? –

+0

我想也許OP想要幫助如何確定動態部分的數量,給定他的樣本數據集,這可能會增長或縮小 – dstepan

0

你可能希望你的數據在一個變量中。

var data = [["name1","name2"],["name3","name4"],["name5","name6"]] 

func numberOfSections(in tableView: UITableView) -> Int { 
    return data.count 
} 

從每個部分獲得的數據(這是未經測試)

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return data[section].count 

} 

要調整,你所提供的代碼,這應該是所有你需要做的

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return allGroups[section].count 
} 
+0

numberOfRowsInSection如何? –

+0

@Johnmayowa我編輯了我的答案。 – dstepan

+0

部分和行從0開始。 –

-1

我想你只需要改變你的代碼的這一部分:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "tablecell") as! TableCell 
    for i in allGroups{ 
     cell.textLabel?.text = i[indexPath.row] 
     for j in i{ 
      print(j) 
     } 
    } 
    return cell 
} 

有了類似的東西

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "tablecell") as! TableCell 

     let group = allGroups[indexPath.section] as! [String] 
     let cellText = group[indexPath.row] 
     cell.textLabel?.text = cellText 

     return cell 
    }