【问题标题】:Saving, retrieving and displaying dynamic table data with Swift使用 Swift 保存、检索和显示动态表数据
【发布时间】:2017-09-15 16:22:48
【问题描述】:

我试图根据存储在 Core Data 中的结果在 Swift 中打开或关闭 UISwitch。

我正在从 json 文件中加载数据,格式如下:

[{
    fruit = Apple;
    id = 01;
}, {
    fruit = Banana;
    id = 02;
}, {
    fruit = Orange;
    id = 03;
}

...它正在用数据填充动态表行。我在行的右侧有一个带有 UISwitch 的重复模板。

像这样:

  • UISwitch“苹果”状态:关闭
  • UISwitch“香蕉”状态:关闭
  • UISwitch“橙色”状态:关闭

我如何定位特定开关以打开或关闭返回的项目以打开或关闭?

我想我需要存储一个我添加到数据集中的 id 数组。例如

fruits = ["02","03"]

...并让代码找到具有该 ID 的行?我对如何去做这件事有点困惑。

加载视图后,我的最终结果将如下所示:

  • UISwitch“苹果”状态:开启
  • UISwitch“香蕉”状态:开启
  • UISwitch“橙色”状态:关闭

...取决于用户选择的内容。

任何帮助将不胜感激。

【问题讨论】:

    标签: json swift uitableview interface-builder


    【解决方案1】:

    假设你的水果数组有这些水果 -

    [{
        fruit = Apple;
        id = 01;
    }, {
        fruit = Banana;
        id = 02;
    }, {
        fruit = Orange;
        id = 03;
    }]
    

    并选择具有这些的水果 ID 数组 -

    ["02","03"]
    

    现在通过在“cellForRowAt”方法中添加这些代码 sn-p 来填充表格行 -

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell") as UITableViewCell!
    
        let fruitsDict = fruitsArray.object(at: indexPath.row) as! NSDictionary
    
        // I've used tag for getting fruit label and fruit switch view, you can use your own custom table view cell here for that.
    
        let lblFruitName = cell.contentView.viewWithTag(1) as! UILabel
        let fruitSwitch = cell.contentView.viewWithTag(2) as! UISwitch
    
        lblFruitName.text = fruitsDict.value(forKey: "fruit") as? String
    
        if(selectedFruitsIDArray.contains(fruitsDict.value(forKey: "id") as? String ?? "")){ // It contains fruit id
    
            fruitSwitch.setOn(true, animated: true)
    
        }
        else{
    
            fruitSwitch.setOn(false, animated: true)
        }
    
       return cell
    }
    

    fruitsArray 是所有水果数组,selectedFruitsIDArray 是选定水果 ID 数组。

    我希望它能帮助你开始。

    【讨论】:

      【解决方案2】:

      如果我们正在考虑您的数据,您有没有想过在您的字典中的每个项目中添加一些布尔类型的 enable 属性。因此,当用户点击单元格或切换此单元格时,您可以将此属性值从false 更改为true,反之亦然。

      user taps on switch 
      change the enable property to true or vice versa
      change switch from off to on or vice versa  
      save into coreData
      

      我将有一个引用类型的数据结构,例如 class,而不是使用 Dictionary,您可以在其中将字典转换为 obj,反之亦然。 (不是必需的,但它让您的生活更轻松,当然,当您保存或更新您将该对象转换为 Dictionary 时)

          class Item {
          var id:Int
          var fruit:String
          var shouldBeEnabled:Bool
      
          init(id:Int, fruit:String, enable:Bool = false) {
            self.id  = id
            self.fruit = fruit
            shouldBeEnabled = enable
          }
          // from dict to item
          static func transform(dict:[String:Any]) -> Item {
              var id = dict["id"] as! Int // use optional instead of explicit unwrapping 
              var fruit = dict["fruit"] as! String
              var enabled = dict["enable"] as! Bool
              return Item(id: id, fruit: fruit, enable: enabled)
          }
      
          // from item to dict 
          static func transform(item:Item) -> [String:Any] {
              var dict = [String:Any]()
              dict["id"] = item.id
              dict["fruit"] = item.fruit
              dict["enable"] = item.shouldBeEnabled
              return dict
           }
      
       }
      

      所以下次用户进入您的应用程序时,开关的状态将保存为启用,然后您正在填充您的 tableView 然后您应该进行检查

       consider you transform all your dict into object 
         if (data[indexPath.row].shouldBeEnabled == true) { // turn on switch }
         else { // turn off } 
      

      现在你的代码有能力:保存用户的更改,并且可以随时加载它

      【讨论】:

      • 谢谢,我会考虑实施这个选项!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      • 2017-06-02
      相关资源
      最近更新 更多