如何在一个自定义tableView单元格中使用两个按钮?我想在单击一个按钮时更改它们的图标

问题描述 投票:0回答:2

这是按钮动作的方法

 @objc func recived()
    {
        kind = false
        self.viewDidLoad()
        self.viewWillAppear(true)

    }
    @objc func paid()
    {
        kind = true
        self.viewDidLoad()
        self.viewWillAppear(true)
    }   

这里有tableView indexPath方法

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

    if kind == true {
        cell.recivedButton.addTarget(self, action:#selector (recived), for: UIControlEvents.touchUpInside)
        cell.recivedButton.setImage(#imageLiteral(resourceName: "green on"), for: UIControlState.normal)
        cell.paidButton.setImage(#imageLiteral(resourceName: "red off"), for: UIControlState.normal)   
    }
    else{
        cell.paidButton.addTarget(self, action: #selector (paid), for: UIControlEvents.touchUpInside)
        cell.paidButton.setImage(#imageLiteral(resourceName: "red on"), for: UIControlState.normal)
        cell.recivedButton.setImage(#imageLiteral(resourceName: "green off"), for: UIControlState.normal)
    }
    return cell
}
ios swift uitableview
2个回答
1
投票

你应该在单元格中处理这个逻辑。在Interface Builder中挂钩您的插座和操作,然后......

class KindTableViewCell: UITableViewCell {

    @IBOutlet private weak var receivedButton: UIButton!
    @IBOutlet private weak var paidButton: UIButton!

    @IBAction func received(_ sender: UIButton) {
        recivedButton.setImage(#imageLiteral(resourceName: "green on"), for: .normal)
        paidButton.setImage(#imageLiteral(resourceName: "red off"), for: .normal)           
    }

    @IBAction func paid(_ sender: UIButton) {
        paidButton.setImage(#imageLiteral(resourceName: "red on"), for: .normal)
        recivedButton.setImage(#imageLiteral(resourceName: "green off"), for: .normal)    
    }
}

注意按钮出口是如何private - 这消除了在视图控制器中更新它们的诱惑。让单元格更新自己的状态。

如果您需要在视图控制器中执行其他工作,请向表视图单元格添加委托协议...

protocol KindTableViewCellDelegate: class {
    func kindTableViewCellDidTapReceive(_ kindTableViewCell: KindTableViewCell)
    func kindTableViewCellDidTapPaid(_ kindTableViewCell: KindTableViewCell)
}

class KindTableViewCell: UITableViewCell {

    // Outlets

    var delegate: KindTableViewCellDelegate?

    @IBAction func received(_ sender: UIButton) {
        // Update button state
        delegate?.kindTableViewCellDidTapReceive(self) 
    }

    @IBAction func paid(_ sender: UIButton) {
        // Update button state
        delegate?.kindTableViewCellDidTapPaid(self) 
    }
}

然后在你的表视图控制器...

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

     cell.delegate = self
     return cell
}

0
投票

您可以根据状态在按钮上添加多个图像:

1:正常状态

    cell.recivedButton.setImage(#imageLiteral(resourceName: "green off"), for: UIControlState.normal)

1:对于选定状态

    cell.recivedButton.setImage(#imageLiteral(resourceName: "green on"), for: UIControlState.selected)

现在,您可以处理按钮状态,按钮根据按钮状态自动选择图像,您必须将此添加到自定义类,如下所示:

override func awakeFromNib() {
        super.awakeFromNib()
        cell.recivedButton.setImage(#imageLiteral(resourceName: "green off"), for: UIControlState.normal)
        cell.recivedButton.setImage(#imageLiteral(resourceName: "green on"), for: UIControlState.selected)
        cell.paidButton.setImage(#imageLiteral(resourceName: "red off"), for: UIControlState.normal)
        cell.paidButton.setImage(#imageLiteral(resourceName: "red on"), for: UIControlState.selected)

    }

现在,您需要在按钮目标方法中处理这样的按钮选择状态:

sender.isSelected = !sender.isSelected

或者可以检查点击paidButton的条件然后付费按钮isSlected为真且recivedButton isSelected为假示例:

@objc func recived(_ sender: UIButton)
    { 
    sender.isSelected = !sender.isSelected 
    paidButton.isSelected = sender.isSelected

    }

@objc func paid(_ sender: UIButton)
    {
    sender.isSelected = !sender.isSelected 
    recivedButton.isSelected = sender.isSelected
    } 
© www.soinside.com 2019 - 2024. All rights reserved.