【问题标题】:how can i save checked rows of tableview in userdefault?如何在 userdefault 中保存已检查的 tableview 行?
【发布时间】:2019-01-25 11:26:02
【问题描述】:

我面临与此问题相同的问题:

how can i store selected rows of tableview in nsuserdefaults in swift 3

但是,我有兴趣知道如何使用已选中的已保存行重新填充?

谢谢!

【问题讨论】:

    标签: userdefaults checkmark


    【解决方案1】:

    使用didSet 创建一个变量,以便我们可以在为其分配值后重新加载表。

    var selectedRows: [Int] = [] {
        didSet {
            myTableView.reloadData()
        }
    }
    

    viewDidLoad() 上,将值从userDefaults 分配给selectedRows

    override func viewDidLoad() {
        super.viewDidLoad()        
        selectedRows = UserDefaults.standard.value(forKey: "selectedRows") as? [Int] ?? []
    }
    

    使用此代码更新单元格。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "myCell")
        cell.textLabel?.text = "\(indexPath.row)"
        cell.accessoryType = selectedRows.contains(indexPath.row) ? .checkmark : .none
        
        return cell
    }
    

    最后在didSelectRowAt使用这个逻辑更新selectedRows变量并将其存储到userDefaults

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if selectedRows.contains(indexPath.row) {
            self.selectedRows = selectedRows.filter{$0 != indexPath.row}
        }else{
            self.selectedRows.append(indexPath.row)
        }
        UserDefaults.standard.set(selectedRows, forKey: "selectedRows")
    }
    

    如果你想在按钮点击时storeuserdefault中选择index,你可以使用下面的例子。

    //
    // Within SMViewController.swift file.
    //
    class SMViewController: UIViewController, UITableViewDataSource {
        
        @IBOutlet weak var myTableView: UITableView!
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 1
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell: SMCell = tableView.smDequeueReusableCell(forIndexPath: indexPath)
            cell.delegate = self
            return cell
        }
        
    }
    
    extension SMViewController: MyCellDelegate {
        func didClickButton(cell: SMCell) {
            if let indexPath = self.myTableView.indexPath(for: cell) {
                // 1) Get all indexes
                var allIndexes = UserDefaults.standard.array(forKey: "myIndexes") as? [Int] ?? []
                // 2) Append current selected index
                allIndexes.append(indexPath.row)
                // 3) Store updated indexes in user defaults.
                UserDefaults.standard.setValue(allIndexes, forKey: "myIndexes")
            }
        }
    }
    
    //
    // Within SMCell.swift file.
    //
    protocol MyCellDelegate: AnyObject {
        func didClickButton(cell: SMCell)
    }
    
    class SMCell: UITableViewCell {
        weak var delegate: MyCellDelegate?
        @IBOutlet weak var myButton: UIButton!
        
        /// Link this button to your `TableViewCell`
        @IBAction func smButtonHandler(_ sender: UIButton) {
            self.delegate?.didClickButton(cell: self)
        }
    }
    

    我希望这会有所帮助。如果您有任何困惑,请告诉我。

    谢谢

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多