2016-03-20 70 views
2

我是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 
} 
+0

您是否設置了委託和數據源? – ridvankucuk

+0

我認爲你在正確的軌道上。什麼是你的問題兄弟? – iMuzahid

+0

我認爲委託和數據源設置正確 - 我能夠填充一個表只有一個部分,但多個部分是問題:我不能找到一種方式來說「讓0節的標題是categoryList中的字符串在一個然後使用categoryDict中爲該特定類別提供的值填充該部分中的單元格「。你懂我的意思嗎?我讀過的所有教程似乎都以不同的方式(我不明白)做到這一點,但是必須要有一種方法來放入數據,因爲我將它從類別詞典中提供出來? – Eirikurii

回答

2

第一折:詞典是用於存儲表視圖內容的差的選擇。字典固有地無序,所以你必須繼續排序鍵。如果您的數據超出了少量項目,排序過程將需要相當長的時間。

如果你打算使用字典,你應該重構你的代碼,這樣你就可以保留已排序的鍵並反覆使用它們,除非字典改變了。我會讓它爲你添加一個setter方法的字典,並始終使用該setter來更改字典。 setter方法將重新生成排序後的鍵。這樣,如果字典更改,您只需對鍵進行排序。 (但更好地擺脫字典完全。)

我會建議創建一個Section對象,其中包含一個部分標題StringsectionEntries數組。然後讓表視圖數據成爲一個Section對象數組。

但是,既然你有一本字典,我會告訴你如何使代碼工作。

您需要幫助您的cellForRowAtIndexPath方法。你幾乎在那裏。您只需要使用indexPath部分和行在您的數據結構中獲取適當的條目。類似這樣的:

override func tableView(tableView: UITableView, 
    cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 

    let cell = tableView.dequeueReusableCellWithIdentifier("BufferCell", 
    forIndexPath: indexPath) 
    let section = indexPath.section 
    let row = indexPath.row 

    let categoryKey:String = categoryList[section] 
    let aCategoryEntry:[String] = categoryDict[category]! 
    let anObject = aCategoryEntry[row] // 
    let cell.textLabel.text = anObject 
    return cell 
} 
+0

這就是我正在尋找的 - 非常感謝你!我使用字典存儲數據的原因很簡單 - 就目前而言 - 我有點理解我在使用它們時做了什麼。效率和數據存儲是我學習迅速完成列表的下一步,但現在我很高興能夠存儲我的數據。再一次:謝謝! – Eirikurii

+0

如果您自己寫了您在原始問題中發佈的代碼,那麼您就有90%的選擇。在最後一步之前,你只是失去了「大局」。 –