0

セグエのない静的セル テーブル ビューがあります。これは、iPhone の [設定] -> [サウンド] -> [テキスト トーン] と同じです。チェックマークを実装して、そのシステム サウンドを別の ViewController から再生しても問題ありません。サウンド設定ViewControllerに戻ると、チェックマークがありません。ユーザーのデフォルトに indexPath.row と indexPath.section を保存しています。これを取得して変数に格納しています。以前に選択された行を示すために、「行用に 1 つ、セクション用に 1 つ」の indexPath を持つこれらの変数を使用するにはどうすればよいですか。Web、ビデオ、および 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

テーブルの選択状態を保持する 1 つの方法は、テーブルに復元識別子文字列を与えることです。これを行うには、テーブル ビューのrestorationIdentifierプロパティを設定するか、Interface Builder のテーブルの ID インスペクターで設定します。

于 2015-09-04T17:04:02.860 に答える