2017-09-15 29 views
0

林显示动态表数据尝试从存储在核心数据导致夫特到打开或关闭一个UISwitch。保存,检索和与夫特

我加载从JSON文件中的数据,格式如下:

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

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

像这样:

  • UISwitch “苹果” 状态:OFF
  • UISwitch “香蕉” 国家:OFF
  • UISwitch “橙色” 状态:OFF

我该如何去针对特定的开关打开或关闭返回的项目以开启?

我想我需要存储的ID,我添加到数据集的数组。例如

fruits = ["02","03"] 

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

我的最终结果是这样的,当视图加载:

  • UISwitch “苹果” 状态:ON
  • UISwitch “香蕉” 国家:ON
  • UISwitch “橙色” 状态:OFF

...根据用户选择的内容。

任何帮助将不胜感激。

回答

1

让我们假设有这些水果的水果阵列 -

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

和选定的水果有这些ID阵列 -

["02","03"] 

所以,现在在你的“cellForRowAt将这些代码片段填充表行'方法 -

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 
} 

其中fru itsArray是你所有的水果阵列和selectedFruitsIDArray选择水果ID阵列。

我希望它会帮助你开始。

+0

谢谢!这指出我在一个伟大的方向! –

+0

不客气。快乐编码:) –

0

如果我们正在考虑您的数据,第二,你有没有想过,在你的字典添加一个boolean类型的一些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,可以在其中变换字典来OBJ反之亦然。 (不是必须的,但它使您的生活更轻松,当您保存或更新您变换对象词典当然)

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 } 

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

+0

谢谢我会考虑实现这个选项! –