gpt4 book ai didi

swift - 在 Swift 中使用 NSUserDefaults 保存和检索 TableViewCell 复选标记

转载 作者:行者123 更新时间:2023-11-28 16:13:45 25 4
gpt4 key购买 nike

我正在尝试使用 NSUserDefaults 保存和检索 tableViewCell 复选标记。我的部分代码如下。从代码中,我可以使用 UITableViewCellAccessoryType 选择或取消选择单元格。我不熟悉在 Swift 中使用 NSUserDefaults。请有人指点我方向……

    override func viewDidLoad() {
super.viewDidLoad()
self.myTableView.allowsMultipleSelection = true
let userDefaults = NSUserDefaults.standardUserDefaults()
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.Checkmark
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.None
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath)
cell.textLabel!.text = myItems[indexPath.row] as? String
cell.selectionStyle = .None
cell.tintColor = UIColor.greenColor()

return cell
}

提前致谢。

最佳答案

  • 首先创建一个类 Item 作为数据源,具有 nameselected 属性。

    class Item {
    let name : String
    var selected = false

    init(name: String) {
    self.name = name
    }
    }
  • 声明数据源数组

    var myItems = [Item]()
  • 以这种方式创建项目

    let item = Item(name:"Foo") // your former string value, `selected` is false by default.
    myItems.append(item)
  • applicationDidFinishLaunching 中注册一个空字符串数组作为键 selectedCells 的默认值

    let defaults = NSUserDefaults.standardUserDefaults()
    let defaultValues = ["selectedCells" : [String]()]
    defaults.registerDefaults(defaultValues)
  • 要从用户默认值中读取所有选定的单元格,请获取字符串数组并将所有对应项的属性 selected 设置为 true。然后重新加载 TableView 。强制展开是安全的,因为键/值是预先注册的并且始终是非可选的。 重要:确保 readDefaults() 始终在注册默认值后被调用。

    func readDefaults()
    {
    let defaults = NSUserDefaults.standardUserDefaults()
    let selectedItems = defaults.stringArrayForKey("selectedCells")!
    for item in myItems {
    item.selected = selectedItems.contains(item.name)
    }
    tableView.reloadData()
    }
  • cellForRowAtIndexPath 中相应地设置两个属性

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath)
    let item = myItems[indexPath.row]
    cell.textLabel!.text = item.name
    cell.accessoryType = item.selected ? .Checkmark : .None
    cell.selectionStyle = .None
    cell.tintColor = UIColor.greenColor()

    return cell
    }
  • 要保存数据筛选所有 selected 属性为 true 的项目,将其映射到名称并保存数组。

    func saveDefaults() {
    let selectedCells = myItems.filter { $0.selected }.map { $0.name }
    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.setObject(selectedCells, forKey:"selectedCells")
    }
  • 现在您应该更改 didSelectRowAtIndexPath 中的模型并重新加载该行。这比操纵单元格更有效(并且推荐)

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let item = myItems[indexPath.row]
    item.selected = true
    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
    }

关于swift - 在 Swift 中使用 NSUserDefaults 保存和检索 TableViewCell 复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39307168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com