首页 > 解决方案 > 如何在表格视图中显示全局字典项

问题描述

我的项目中有两个视图:在第一个视图中,用户输入两个单词,这些单词作为键和值保存在字典中。简而言之,代码:

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).")
    }
}

但它只显示最后添加的值,而在第一个代码中它为每个新值创建一个新单元格。

我做错了什么?

标签: iosswiftxcodedictionary

解决方案


您访问数组索引 = 您需要的项目数

var wordIndex1 = words.count - 1

或简而言之

if let lastWord =  words.last {
  ////
}

注意数组索引从 0 开始到 numberOfElemnts - 1


另外最好有模型

struct Item {
  let leftInp.rightInp:String
}

然后像这样创建数组

var arr = [Item]()

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return words.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!

    let dic = words[indexPath.row]

    cell.textLabel?.text = dic.keys.first

    return cell
}

推荐阅读