分别从所有 UITableViewCells 获取数据

Get data from all UITableViewCells separately

我有一个 returns 四个单元格的 TableViewController。每个单元格中都有三个按钮。单元格本身没有任何交互或连接,因为我不需要用户与单元格交互,只需要单元格中的三个按钮。

我有一个名为 selections 的空数组,如果按下每个按钮,我想将一个项目附加到该数组。到目前为止,我找不到任何方法来跟踪按下的每个按钮。

我可以将此代码放入什么方法?

if cell.yes.isSelected == true {
            firstChoice = 1
            selections.append(firstChoice)
            print(selections.count, selections.reduce(0, +))
        }

以便它适用于我的 TableViewController 加载的所有单元格?

首先 - 您应该在 ViewController 中保留有关按下按钮的信息 - 而不是在 table 单元格中。 Table 个单元格将被重复使用 - 您将丢失该信息。 最好的方法是在单元格和 TableViewController 之间使用自定义委托。在创建每个单元格时,您需要:

cell.delegate = self

并在按下按钮时在单元格内调用此委托方法 - 比方说 didPressButton1 didPressButton2.

此外,如果您希望在 TableViewController 中创建单元格时保持此状态(例如禁用、启用某些按钮),则需要提取现有数据并应用它到单元格本身 - 再次 TableViewCells 被重用。

我不知道你的规格,但你似乎可以为每个按钮添加不同的目标:

button1?.addTarget(self, action:#selector(self.button1Clicked), forControlEvents: .TouchUpInside)
...
button3?.addTarget(self, action:#selector(self.button3Clicked), forControlEvents: .TouchUpInside)

然后你可以:

func button1Clicked() {
    firstChoice = 1
    selections.append(firstChoice)
}

...

func button3Clicked() {
    firstChoice = 3
    selections.append(firstChoice)
}

这样,如果按钮 1 被点击,button1Clicked() 就会被触发,您可以按预期进行工作。

为了实现您想要的效果,您需要为自定义单元格和按钮创建委托。

//here is the view controller class
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CustomCellDelegate {

    var firstChoice = 0
    var selections = Array<Any>()

    @IBOutlet weak var tableView: UITableView!
        override func viewDidLoad() {
            super.viewDidLoad()


    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomCell

        cell?.delegate = self

        return cell!
    }

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

    func cellButtonTapped(_ cell: CustomCell) {
        if cell.isSelected {
            firstChoice = 1
            selections.append(firstChoice)
        }
    }
 }

自定义单元格class

protocol CustomCellDelegate {
    func cellButtonTapped(_ cell: CustomCell)
}

class CustomCell: UITableViewCell, CustomButtonDelegate {

    var delegate: CustomCellDelegate?

    @IBOutlet weak var button1: CustomButton!
    @IBOutlet weak var button2: CustomButton!
    @IBOutlet weak var button3: CustomButton!

    override func awakeFromNib() {
        super.awakeFromNib()

        button1.delegate = self
        button2.delegate = self
        button3.delegate = self
    }

    func buttonTapped() {
        self.isSelected = !self.isSelected

        if let delegate = delegate {
            delegate.cellButtonTapped(self)
        }
    }
}

和自定义按钮

protocol CustomButtonDelegate{
    func buttonTapped()
}

class CustomButton: UIButton {

    var delegate: CustomButtonDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()

        self.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
    }

    func buttonTapped(_ sender: AnyObject) {
        if let delegate = delegate {
            delegate.buttonTapped()
        }
    }
}

他们都有自己的协议