我怎样才能从一个字典快速分组TableView项目?

让我们考虑这个例子:

import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! var names = ["Vegetables": ["Tomato", "Potato", "Lettuce"], "Fruits": ["Apple", "Banana"]] func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:"test") return cell } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return ??? } func numberOfSectionsInTableView(tableView: UITableView) -> Int{ return names.count } func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]!{ return ??? } func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?{ return ???? } } 

假设我们需要字典的关键字(水果和蔬菜)是分段的数量,再加上这些分段的标题。 键(如苹果和香蕉)的项目将是每个部分的行。 我如何在我的代码中实现这个? 我知道这可能很容易,但我无法弄清楚我的自我。

你可以使用结构,这里是例子:

 import UIKit class TableViewController: UITableViewController { var names = ["Vegetables": ["Tomato", "Potato", "Lettuce"], "Fruits": ["Apple", "Banana"]] struct Objects { var sectionName : String! var sectionObjects : [String]! } var objectArray = [Objects]() override func viewDidLoad() { super.viewDidLoad() for (key, value) in names { println("\(key) -> \(value)") objectArray.append(Objects(sectionName: key, sectionObjects: value)) } } // MARK: - Table view data source override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return objectArray.count } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return objectArray[section].sectionObjects.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell // Configure the cell... cell.textLabel?.text = objectArray[indexPath.section].sectionObjects[indexPath.row] return cell } override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return objectArray[section].sectionName } } 

Swift 2

你字典的例子

 var dic:Dictionary<String,String> = ["key":"value","key1":"value2"] 

你的桌子

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell var key = Array(self.dic.keys)[indexPath.row] var value = Array(self.dic.values)[indexPath.row] cell.text = key + value } 

来自Apple文档:

var keys: LazyForwardCollection<MapCollectionView<Dictionary<Key, Value>, Key>> { get }

Description :只包含自我的键的集合。 键的出现顺序与它们作为自我中的键值对的.0成员相同。 结果中的每个键都有唯一的值。

names.keys.array返回一个键的Array

所以:

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return names.keys.array[section].count } func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]!{ return names.keys.array } func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?{ return names.keys.array[section] } 

这将适用于任何字典与任何数据量(即使它是程序员不知道的

所有集合types都必须是Array

 var names = [["Tomato", "Potato", "Lettuce"], ["Apple", "Banana"]] var sectionNames = ["Vegetables", "Fruits"] func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return names[section].count } func numberOfSectionsInTableView(tableView: UITableView) -> Int{ return names.count } func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]!{ return sectionNames } func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?{ return sectionNames[section] } 

如果你想sorting,使用全局sorting函数来sorting字典。

 import UIKit class TableViewController: UITableViewController { var names = ["Vegetables": ["Tomato", "Potato", "Lettuce"], "Fruits": ["Apple", "Banana"]] var namesSorted = [String, Array<String>]() override func viewDidLoad() { super.viewDidLoad() // Sort names namesSorted = sorted(names) { $0.0 < $1.0} // namesSorted = ["Fruits": ["Apple", "Banana"], "Vegetables": ["Tomato", "Potato", "Lettuce"]] } // MARK: - Table view data source override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return namesSorted.count } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return namesSorted[section].1.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell // Configure the cell... cell.textLabel?.text = namesSorted[indexPath.section].1[indexPath.row] return cell } override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return namesSorted[section].0 } }