gpt4 book ai didi

swift - 返回表格 View 时复选标记消失

转载 作者:行者123 更新时间:2023-11-30 14:05:59 25 4
gpt4 key购买 nike

我有一个没有转场的静态单元格表​​ View 。这与 iPhone 的“设置”->“声音”->“文本铃声”相同。实现复选标记并从另一个 ViewController 播放系统声音没有问题。当返回到声音设置 ViewController 时,没有复选标记。我将indexPath.row 和indexPath.section 保存在用户默认值中。我正在检索它并将它们存储在变量中。如何使用现在具有索引路径“一个用于行,一个用于部分”的变量来指示先前选择的行。我已经在网络、视频和 StackoverFlow 上尝试过该解决方案,但我似乎无法理解这一点。

var rowSelected:Int = 0
var rowSection:Int = 0

override func viewDidLoad() {
super.viewDidLoad()
var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()

if let soundIsNotNill = defaults.objectForKey("rowSelectedKey") as? Int{
self.rowSelected = defaults.objectForKey("rowSelectedKey") as! Int}

if let soundIsNotNill = defaults.objectForKey("rowSectionKey") as? Int{
self.rowSection = defaults.objectForKey("rowSectionKey") as! Int}

}

override func viewWillAppear(animated: Bool) {
println(" VDL rowSelected \(rowSelected)")
println(" VDL rowSection \(rowSection)")

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 13
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let tappedItem = indexPath.row
rowSelected = tappedItem
let section = indexPath.section
rowSection = section

for row in 0..<13 {
if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: row, inSection: section)) {
cell.accessoryType = row == tappedItem ? .Checkmark : .None
}
}



println("didSelectRow rowSelected \(rowSelected)")
println("didSelectRow rowSection \(rowSection)")

var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(section, forKey: "rowSectionKey")
defaults.setObject(tappedItem, forKey: "rowSelectedKey")
defaults.synchronize()

saveSound()
}

最佳答案

保留表选择状态的一种方法是为其提供恢复标识符字符串。您可以通过设置 TableView 的 restorationIdentifier 属性或在 Interface Builder 中表的身份检查器中进行设置来完成此操作。

关于swift - 返回表格 View 时复选标记消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32402958/

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