【问题标题】:Storing String and Int in a dictionary in swift将String和Int快速存储在字典中
【发布时间】:2018-06-12 14:08:45
【问题描述】:

我几乎是 Swift 的新手。 In this URL我会得到一些元素;其中一个元素是 categoryList,它本身有两个元素。我将goodTypeName设置为表格的单元格标题,当按下一个单元格时,它需要发送goodType,它是数字(Int)以放置在下一个Url中。我尝试创建字典,但失败了!

UiTable 代码:::

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return Global.GlobalVariable.names.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if Global.GlobalVariable.names == []
    {
        self.DisplayMessage(UserMessage: "nothing is available ")
        print("server is nil")
    }
    let cell = UITableViewCell()
    let content = Global.GlobalVariable.names[indexPath.row]
    cell.textLabel?.text = content
    cell.accessoryType = .disclosureIndicator
    return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(indexPath.item)
    let next = self.storyboard?.instantiateViewController(withIdentifier: "SVC")
    self.navigationController?.pushViewController(next!, animated: true)
}

My problem is not with populating them in a table, my problem is when a cell is selected , goodtype is needed to be sent to next page, becuase next page's url has to have the goodtype code.

【问题讨论】:

  • 带有代码的描述具有误导性,添加您迄今为止尝试过的内容并详细说明
  • 我更新了描述

标签: json swift uitableview dictionary


【解决方案1】:

您可以使用“prepareSegue”来传递数据。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if segue.identifier == "something"{
    if let startViewController = segue.destination as? StartViewController{
        startViewController.goodtype = Global.GlobalVariable.goodtype[indexPath.row]
    }
}
}

在您的 StartViewController 中只需分配一个变量来接收您的数据:

var goodtype = String()

使用导航控制器,但通过这一行,您可以访问另一个视图控制器属性。

         if let startViewController = storyboard.instantiateViewController(withIdentifier: "StartViewController") as? StartViewController {
    startViewController.goodtype = Global.GlobalVariable.goodtype[indexPath.row]
     let navigationController = UINavigationController()
  navigationController.pushViewController(startViewController, animated: true)
    }

【讨论】:

  • 我的问题不是传递数据,我的问题是我无法识别每个单元格的好类型!我自己认为我需要一本字典
  • ok 好类型存储在哪里?您还可以使用类或结构或字典,然后使用 indexpath.row 只需选择所选单元格的项目。
  • 我有一个全局变量,我要把它们存储在那里!如果这对您来说可能,请检查邮递员中的网址以了解我的意思!我也对我的问题感到困惑:|
  • struct mycalss { var goodtype = Int() var goodTypeName = String() } var mydata = [mycalss]() var myclass= mycalss.init(goodtype: 101, goodTypeName: "کنجاله سوي") mydata.append(myclass) mydata[0].goodTypeName mydata[0].goodtype 你有很多选择这是最快的,只需创建一个结构并创建一个结构数组,将数据附加到你的结构然后你可以用 indexpath.row 你的数据分配它
  • 当一个单元格被选中时我应该如何设置" var myclass= mycalss.init(goodtype: 101, goodTypeName: "کنجاله سوي") "
猜你喜欢
  • 2019-01-31
  • 1970-01-01
  • 1970-01-01
  • 2020-02-02
  • 2014-10-02
  • 2015-11-22
  • 2018-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多