0

我有一个没有 segue 的静态单元格表​​视图。这与 iPhone 的设置->声音->文本音相同。实现复选标记并从另一个 ViewController 播放系统声音没有问题。返回声音设置 ViewController 时没有复选标记。我将 indexPath.row 和 indexPath.section 保存在用户默认值中。我正在检索它并将它们存储在变量中。我如何使用这些现在具有 indexPath“一个用于行,一个用于部分”的变量来指示先前选择的行。我已经在网络、视频和 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()
}
4

1 回答 1

0

保存表的选择状态的一种方法是给它一个恢复标识符字符串。您可以通过设置表视图的restorationIdentifier属性或在 Interface Builder 中的表的身份检查器中设置它来做到这一点。

于 2015-09-04T17:04:02.860 回答