簡體   English   中英

從Swift中的另一個類訪問某些值

[英]Access to some values from another class in Swift

我有一個這樣的TableViewCell類:

class CampaignsTableViewCell: UITableViewCell {

    @IBOutlet weak var activateButton: UIButton!
    @IBOutlet weak var titleCampaignPlaceholder: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        setUpButton()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }

    private func setUpButton(){
        activateButton.backgroundColor = .clear
        activateButton.layer.cornerRadius = 5
        activateButton.layer.borderWidth = 1
        activateButton.layer.borderColor = UIColor.blue.cgColor
    }
}

而且,在另一個類是ViewController中,我有UITableView方法:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let rowNumber = indexPath.row
    let cellIdentifier = "CampaignTableViewCell"
    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? CampaignsTableViewCell else {
        fatalError("The dequeued cell is not an instance of TableViewCellController.")
    }

    cell.titleCampaignPlaceholder.text = campaignsArray[rowNumber].campaignName


    return cell
}

我需要在UITableView方法中使用ActivateButton才能訪問campaignsArray。 我有另一個方法需要從該數組中獲取值,因此我需要在每次從UITableView中按下activateButton時都調用該方法。

任何想法 ? 非常感謝你

在UITableViewCell中有一個按鈕的情況下,我喜歡做的事情如下:

給單元格一個閉合,當點擊按鈕時調用該閉合

class CampaignsTableViewCell: UITableViewCell {
   ... all your code....

   // give your cell a closure that is called when the button is pressed    
   var onButtonPressed: ((_ sender: UIButton) -> ())?

   @IBAction func buttonPressed(sender: UIButton) {  // wire that one up in IB
       onButtonPressed?(sender)
   }
 }

然后在您的TableViewController內部

let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! CampaignsTableViewCell

cell.titleCampaignPlaceholder.text = campaignsArray[rowNumber].campaignName

cell.onButtonPressed = { [weak self] sender in
    // Do your magic stuff here
}

return cell

希望能有所幫助

您的單元格將獲取該事件,而不是tableView 您需要做的是:

在您的單元內創建協議:

protocol CampaignsTableViewProtocol{
    func actionButtonPressed(row: Int)
}

class CampaignsTableViewCell: UITableViewCell {

    @IBOutlet weak var activateButton: UIButton!
    @IBOutlet weak var titleCampaignPlaceholder: UILabel!
    // keep info about row 
    var rowIndex: Int = -1
    // create delegate that will let your tableView about action button in particular row
    var delegate : CampaignsTableViewProtocol?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        setUpButton()
        self. activateButton.addTarget(self, action: #selector(self.activatePressed), for: UIControlEvents.touchDown)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }

    func activatePressed(){
         self.delegate?.actionButtonPressed(row :rowIndex)
    }

    private func setUpButton(){
        activateButton.backgroundColor = .clear
        activateButton.layer.cornerRadius = 5
        activateButton.layer.borderWidth = 1
        activateButton.layer.borderColor = UIColor.blue.cgColor
    }
}

您的tableViewController需要采用以下協議:

class MyTableViewController: UITableViewDelegate, UITableViewDataSource, CampaignsTableViewProtocol {
// rest of the code
}

另外,您將需要在tableViewController中實現委托函數:

func actionButtonPressed(row: Int) {
    // get campaign you need
    let campaign = campaignsArray[row]
    // rest of the code
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM