简体   繁体   English

按下按钮时,用户默认在 TableView 中保存单元格

[英]UserDefault save cell in TableView when button is pressed

I use UserDefault to stored an image that I have in TableView cell, I would like to know how can stored what is in the cell, when the button is pressed我使用 UserDefault 来存储我在 TableView 单元格中的图像,我想知道当按下按钮时如何存储单元格中的内容

Thank you so much太感谢了

class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

var imageArray = [UIImage(named: "image1"), UIImage(named: "image2"),
                  UIImage(named: "image3"), UIImage(named: "image4"),
                  UIImage(named: "image5"), UIImage(named: "image6")]

var userDefault = UserDefaults.standard

override func viewDidLoad() {
    super.viewDidLoad()


}

@IBAction func imageRoll(_ sender: UIBarButtonItem) {
    _ = imageArray.shuffle()
    tableView.reloadData()
}

@IBAction func addImage(_ sender: UIBarButtonItem) {
    imageArray.insert(UIImage(named: "image1"), at: 0)
    tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .automatic)
}


@IBAction func saveConfig(_ sender: UIBarButtonItem) {

}

func saveData(){


}
}


extension ViewController: UITableViewDataSource, UITableViewDelegate {

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return imageArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: “ImageCell", for: indexPath) as! ImageTableViewCell
    let images = imageArray[indexPath.row]

    cell.imageCell.image = images

    return cell
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == .delete {
        imageArray.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .automatic)
    }
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 40
}

}

Firstly declare tableview cell globally:-首先全局声明tableview单元格:-

var cell : ImageTableViewCell?

Create image array to save in user default:-创建图像数组以保存在用户默认值中:-

var saverImageArr = [UIImage?]()

Now when button is pressed:-现在按下按钮时:-

@IBAction func imageRoll(_ sender: UIBarButtonItem) {
    for image in 0..<imageArray.count {
        if saverImageArr.count > 0
        {
            saverImageArr.removeAll()
        }

        saverImageArr.append(cell.imageCell.image)
    }
    UserDefaults.standard.set(saverImageArr, forKey: "imageArray")
    UserDefaults.standard.synchronize()
    tableView.reloadData()
}

Now to get that array from user default:-现在从用户默认获取该数组:-

if let imageArrfromUserdefault = UserDefaults.standard.array(forKey: "imageArray")
   {
        print(imageArrfromUserdefault)
   }
   else
   {
        print("empty array")
   }

Thank you.谢谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM