【发布时间】:2016-07-06 22:02:29
【问题描述】:
我是 Swift 的初学者,并且至少了解了如何填充 UITableView 的基本知识。我现在被困在用字典提供的数据填充多个部分。
我有一个将对象分配给类别的多维字典:
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 部分的标题是 categoryList 中特定索引处的字符串,然后使用 categoryDict 中为该特定类别提供的值填充该部分中的单元格”。你懂我的意思吗?我读过的所有教程似乎都以不同的方式做到这一点(我不太明白),但是当我从类别字典中提供数据时,必须有一种方法来输入数据?跨度>
标签: ios swift uitableview