【发布时间】:2019-03-22 22:55:29
【问题描述】:
我的项目中有两个视图:在第一个视图中,用户输入两个单词,这些单词作为键和值保存在字典中。代码简述:
var words = [Dictionary<String, String>]()//<-declared outside the class
words.append([input.text: input2.text] as! [String : String])
然后在第二个视图中,我想在表格单元格中显示所有保存的单词,如下所示:
我已经找到了一些代码,但它只适用于本地字典:
class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// Data model: These strings will be the data for the table view cells
let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]
// cell reuse id (cells that scroll out of view can be reused)
let cellReuseIdentifier = "cell"
// don't forget to hook this up from the storyboard
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Register the table view cell class and its reuse id
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
// (optional) include this line if you want to remove the extra empty cell divider lines
// self.tableView.tableFooterView = UIView()
// This view controller itself will provide the delegate methods and row data for the table view.
tableView.delegate = self
tableView.dataSource = self
}
// number of rows in table view
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.animals.count
}
// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// create a new cell if needed or reuse an old one
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
// set the text from the data model
cell.textLabel?.text = self.animals[indexPath.row]
return cell
}
// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You tapped cell number \(indexPath.row).")
}
}
我已经像这样编辑了上面的代码:
var wordIndex1 = words.count - 1
class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var word = words[wordIndex1] as? Dictionary<String, String>
let cellReuseIdentifier = "cell"
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (self.word?.count)!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
cell.textLabel?.text = self.word?.keys.first
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You tapped cell number \(indexPath.row).")
}
}
但它只显示最后添加的值,而在第一个代码中它为每个新值创建一个新单元格。
我做错了什么?
【问题讨论】:
-
你有一个字典数组,每个字典包含 1 个键/值,所以这个
var word = words[wordIndex1] as? Dictionary<String, String>有 count = 1 -
哦,我现在终于明白了.. 那我怎样才能显示所有这些呢?也许为字典键创建一个数组,为字典值创建另一个数组?
-
您确实需要遵循
struct的方式,但请参阅下面的编辑 -
使用快速元组
标签: ios swift xcode dictionary