使用 Swift 保存、检索和显示动态 table 数据
Saving, retrieving and displaying dynamic table data with Swift
我正在尝试根据存储在 Core Data 中的结果在 Swift 中打开或关闭 UISwitch。
我正在从 json 文件加载数据,格式如下:
[{
fruit = Apple;
id = 01;
}, {
fruit = Banana;
id = 02;
}, {
fruit = Orange;
id = 03;
}
...它正在用数据填充动态 table 行。我有一个重复模板,在行的右侧有一个 UISwitch。
像这样:
- UISwitch "Apple" 状态:关闭
- UISwitch "Banana" 状态:关闭
- UISwitch "Orange" 状态:关闭
我如何定位特定开关以打开或关闭返回的项目以打开?
我想我需要存储一个我添加到数据集中的 id 数组。例如
fruits = ["02","03"]
...并让代码查找具有该 ID 的行?我对如何去做这件事有点困惑。
加载视图后,我的最终结果如下所示:
- UISwitch "Apple" 状态:ON
- UISwitch "Banana" 状态:ON
- UISwitch "Orange" 状态:关闭
...取决于用户选择的内容。
如有任何帮助,我们将不胜感激。
假设你的水果数组有这些水果 -
[{
fruit = Apple;
id = 01;
}, {
fruit = Banana;
id = 02;
}, {
fruit = Orange;
id = 03;
}]
并选择具有这些的水果 ID 数组 -
["02","03"]
现在通过在 'cellForRowAt' 方法中添加这些代码片段来填充 table 行 -
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 数组。
希望对您有所帮助。
如果我们考虑一下您的数据,您是否考虑过在词典的每个项目中添加一些 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
,您可以在其中将字典转换为对象,反之亦然。 (不是必需的,但它会让您的生活更轻松,当然,当您保存或更新时,您将该对象转换为字典)
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 }
Now your code has the ability: save user's change, and can load it anytime
我正在尝试根据存储在 Core Data 中的结果在 Swift 中打开或关闭 UISwitch。
我正在从 json 文件加载数据,格式如下:
[{
fruit = Apple;
id = 01;
}, {
fruit = Banana;
id = 02;
}, {
fruit = Orange;
id = 03;
}
...它正在用数据填充动态 table 行。我有一个重复模板,在行的右侧有一个 UISwitch。
像这样:
- UISwitch "Apple" 状态:关闭
- UISwitch "Banana" 状态:关闭
- UISwitch "Orange" 状态:关闭
我如何定位特定开关以打开或关闭返回的项目以打开?
我想我需要存储一个我添加到数据集中的 id 数组。例如
fruits = ["02","03"]
...并让代码查找具有该 ID 的行?我对如何去做这件事有点困惑。
加载视图后,我的最终结果如下所示:
- UISwitch "Apple" 状态:ON
- UISwitch "Banana" 状态:ON
- UISwitch "Orange" 状态:关闭
...取决于用户选择的内容。
如有任何帮助,我们将不胜感激。
假设你的水果数组有这些水果 -
[{
fruit = Apple;
id = 01;
}, {
fruit = Banana;
id = 02;
}, {
fruit = Orange;
id = 03;
}]
并选择具有这些的水果 ID 数组 -
["02","03"]
现在通过在 'cellForRowAt' 方法中添加这些代码片段来填充 table 行 -
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 数组。
希望对您有所帮助。
如果我们考虑一下您的数据,您是否考虑过在词典的每个项目中添加一些 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
,您可以在其中将字典转换为对象,反之亦然。 (不是必需的,但它会让您的生活更轻松,当然,当您保存或更新时,您将该对象转换为字典)
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 }
Now your code has the ability: save user's change, and can load it anytime