使用按钮保存 TableView 单元格数据(包含段控制器)

Saving TableView cells data (containing segment controller) using button

我已经厌倦了搜索我想要的东西,所以我会在这里问,如果可能的话希望你们能帮忙。

我有一个 table 视图包含每个单元格中的段,我想使用按钮 [在 table 视图外] 保存所有单元格段,以便我可以在另一个 table 稍后。

Here is my tableview

这是我的视图控制器:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    let arr = ["item 1",
               "item 2",
               "item 3"]

    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.register(CustomCell.self, forCellReuseIdentifier: "cell")
    }
    
    @IBAction func saveButtonAction(_ sender: UIButton) {
        
        // I want to save the cells segmented control selectedSegmentIndex???
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        90
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
        cell.textView.text = arr[indexPath.row]
        return cell
    }
}

这是我的自定义单元格,它包含 TextView 和 Segment Controller 我试图将段更改保存在数组中,但我不知道在大声笑之后要做什么:

 import UIKit

 class CustomCell: UITableViewCell {

    let textView: UITextView = {
        let tv = UITextView()
        tv.translatesAutoresizingMaskIntoConstraints = false
        tv.font = UIFont.systemFont(ofSize: 16)
        tv.isEditable = false
        return tv
    }()
    
    // var rowIndexPath: Int?
    var segmentArray: [Int] = []
    
    lazy var itemSegmentedControl: UISegmentedControl = {
        let sc = UISegmentedControl(items: ["1", "2", "3"])
        sc.translatesAutoresizingMaskIntoConstraints = false
        sc.tintColor = UIColor.white
        sc.selectedSegmentIndex = 0
        sc.addTarget(self, action: #selector(handlePointChange), for: .valueChanged)
        return sc
    }()
    
    let stackView: UIStackView = {
        let sv = UIStackView()
        sv.translatesAutoresizingMaskIntoConstraints = false
        return sv
        
    }()
    
    @objc func handlePointChange() {
        
        let segChange = itemSegmentedControl.titleForSegment(at: itemSegmentedControl.selectedSegmentIndex)!
        segmentArray.append(Int(segChange)!)
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        stackView.addSubview(textView)
        stackView.addSubview(itemSegmentedControl)
        addSubview(stackView)
        
        stackView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        stackView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        stackView.widthAnchor.constraint(equalToConstant: 400).isActive = true
        stackView.heightAnchor.constraint(equalToConstant: 85).isActive = true
        
        itemSegmentedControl.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        itemSegmentedControl.topAnchor.constraint(equalTo: textView.bottomAnchor, constant: 5).isActive = true
        itemSegmentedControl.widthAnchor.constraint(equalTo: textView.widthAnchor).isActive = true
        itemSegmentedControl.heightAnchor.constraint(equalToConstant: 40).isActive = true
        
        textView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -10).isActive = true
        textView.topAnchor.constraint(equalTo: stackView.topAnchor).isActive = true
        textView.widthAnchor.constraint(equalTo: stackView.widthAnchor).isActive = true
        textView.heightAnchor.constraint(equalToConstant: 40).isActive = true
    }
 }

问候和感谢。

首先:在 ViewController 我添加了空数组:

// I moved the segment array from the custom cell class to here
var segmentArray: [Int:Int] = [:]

override func viewDidLoad() {
    super.viewDidLoad()
    
    // I made this array to store default state to the items segments
    segmentArray = [0:0,1:0,2:0] 
}

// with this func I can update the segmentArray
func getSegmentNumber(r: Int, s: Int) {
    segmentArray[r] = Int(s)
}

@IBAction func saveButtonAction(_ sender: UIButton) {
    // Here I can save the new state for the segment to the firebase or anywhere.
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
    cell.textView.text = arr[indexPath.row]

    // here to store the index path row
    cell.rowIndexPath = indexPath.row

    // you need to add the link because it won't work
    cell.link = self
    return cell
}

第二个:在自定义单元格中Class:

// I removed the store array
var segmentArray: [Int] = [] // removed

// I made link to the ViewController
var link: ViewController?

// variable to get the index path row
var rowIndexPath: Int?

// here I can update the state of the segment using the link
@objc func handlePointChange() {
    
    let segChange = itemSegmentedControl.selectedSegmentIndex
    link?.getSegmentNumber(r: rowIndexPath!, s: segChange)
}

这对我有用,我希望你明白了。 有什么需要可以问。

再次感谢 Kudos。 问候大家