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

标签 ios swift uitableview

这是按钮操作的方法

 @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
}

最佳答案

您应该在单元格本身中处理此逻辑。在 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)    
    }
}

注意按钮 socket 是如何私有(private)的——这消除了在 View Controller 中更新它们的诱惑。让细胞更新自己的状态。

如果您需要在 View Controller 中做额外的工作,请将委托(delegate)协议(protocol)添加到您的 TableView 单元格中......

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) 
    }
}

然后在你的 TableView Controller 中......

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

     cell.delegate = self
     return cell
}

关于ios - 如何在一个自定义 tableView 单元格中使用两个按钮?我想在单击一个按钮时更改它们的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50315165/

相关文章:

ios - 无论语言环境/isDaylightSavingTime/时区如何,DateFormatter 都会为某些用户返回 nil 日期

ios - 来自 plist 的 UITableView 加载到字典的 NSArray 中

ios - 无法获取 UITableViewCell 的索引路径

ios - 快速从文件中减去并更新

arrays - 是否可以在 Swift 中创建一个仅限于一个类的数组扩展?

iphone - UITableViewCell 内的 UIWebView 闪烁白色并且大小不正确

swift - 我在更新 xcode 后收到此错误 "No such module ' SwiftyJSON'

objective-c - 具有多个部分时使用 sectionIndexTitlesForTableView 进行索引

ios - Swift UITableView 不显示所有结果

ios - 单击更改行按钮图像源?