我是Swift的初學者,已經理解了至少如何填充UITableView的基礎知識。我現在被困在使用字典提供的數據填充多個部分。Swift - 從多維數組中填充多個部分的TableViews
我有一個多維字典,分配對象類別:
var categoryDict:[String:[AnyObject]] = ["Category A": ["Object 1","Object 2"], "Category B":["Object 3"]]
現在我想填充我的TableView,所以它顯示是這樣的:
A類
- 對象1
- 對象2
B類
- 對象3
到目前爲止,我可以創建類別的數組返回部分的數量以及計算陣列存儲在字典中以獲取每個特定類別中的行數。現在我完全停留在用表格和行填充TableView。我怎樣才能做到這一點?非常感謝你!
到目前爲止我的代碼:
var categoryList:[String] = [String]()
var categoryDict:[String:[AnyObject]] = ["Category A": ["Object 1","Object 2"], "Category B":["Object 3"]]
func getLists() {
categoryList = Array(categoryDict.keys)
categoryList.sortInPlace(before)
}
// Sort Array
func before(value1: String, value2: String) -> Bool {
return value1 < value2;
}
func getNumberOfEntrysInSection (Section: Int) -> Int {
let category:String = categoryList[Section] //Get Category for Index in CategoryList
let value:[AnyObject] = categoryDict[category]! //Get Value for this specific Category
let number = value.count
return number
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
getLists()
return categoryList.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return getNumberOfEntrysInSection(section)
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return categoryList[section]
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("BufferCell", forIndexPath: indexPath)
???
return cell
}
您是否設置了委託和數據源? – ridvankucuk
我認爲你在正確的軌道上。什麼是你的問題兄弟? – iMuzahid
我認爲委託和數據源設置正確 - 我能夠填充一個表只有一個部分,但多個部分是問題:我不能找到一種方式來說「讓0節的標題是categoryList中的字符串在一個然後使用categoryDict中爲該特定類別提供的值填充該部分中的單元格「。你懂我的意思嗎?我讀過的所有教程似乎都以不同的方式(我不明白)做到這一點,但是必須要有一種方法來放入數據,因爲我將它從類別詞典中提供出來? – Eirikurii