简体   繁体   English

从 ViewController Swift 中的 UITableViewCell 调用 function

[英]Call function from UITableViewCell from in ViewController Swift

I need to call function deleteButtonShowHide , which is in TeamsCell, from TeamsVC, when plusBtnTapped .plusBtnTapped时,我需要从 TeamsVC 调用位于 TeamsCell 中的 function deleteButtonShowHide I am trying to figure it out with protocol TeamsVCDelegate , but it doesn't work( It works vice versa for me. But I do not know how to implement something like cell.teamsCellDelegate = self我正在尝试使用协议 TeamsVCDelegate来解决它,但它不起作用(它对我来说反之亦然。但我不知道如何实现类似cell.teamsCellDelegate = self

TeamsCell团队细胞

import UIKit

protocol TeamsCellDelegate {
    func deleteCell()
}



class TeamsCell: UITableViewCell {
    
    @IBOutlet weak var teamNameLbl: UILabel!
    @IBOutlet weak var deleteButton: UIButton!
    var teamsCellDelegate: TeamsCellDelegate?
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    
    func updateCell(team: team) {
        teamNameLbl.text = team.name
    }
    
    
    @IBAction func deleteButtonTapped(_ sender: Any) {
        debugPrint("delet tapped")
        //deleteButtonShowHide()
        findAndDeleteTeam()
        teamsCellDelegate?.deleteCell()
    }
    
    func findAndDeleteTeam() {
        for i in 0...teams.count - 1 {
            if teams[i].name == teamNameLbl.text {
                teams.remove(at: i)
                break
            }
        }
    }
    
    func deleteButtonShowHide(){
        if teams.count < 3 {deleteButton.isHidden = true}
        if teams.count > 2 {deleteButton.isHidden = false}
    }
    
}

extension TeamsCell: TeamsVCDelegate {
    func deleteButtonSH() {
        debugPrint("XXX")
        deleteButtonShowHide()
    } 
}

TeamsVC团队VC

import UIKit

protocol TeamsVCDelegate {
    func deleteButtonSH()
}

class TeamsVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
   
    @IBOutlet weak var plusBtn: UIButton!
    @IBOutlet weak var teamsTable: UITableView!
    var teamsVCDelegate: TeamsVCDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        teamsTable.delegate = self
        teamsTable.dataSource = self
        teamsTable.rowHeight = 55
        teamsTable.isScrollEnabled = false
        teamsTable.backgroundColor = nil
        teamsTable.separatorStyle = .none
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return teams.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "TeamsCell") as? TeamsCell {
            cell.updateCell(team: teams[indexPath.row])
            cell.teamsCellDelegate = self
            return cell
        }
        return UITableViewCell()
    }
    
    @IBAction func plusBtnTapped(_ sender: Any) {
        plusBtnHide()
        addTeam()
        teamsTable.reloadData()
        teamsVCDelegate?.deleteButtonSH()
        print(teams)
    }
    
    func plusBtnShow() {
        if teams.count < 5 {plusBtn.isHidden = false}
    }
    
    func plusBtnHide() {
        if teams.count == 4 { plusBtn.isHidden = true}
    }
    
}

extension TeamsVC: TeamsCellDelegate {
    func deleteCell() {
        self.teamsTable.reloadData()
        self.plusBtnShow()
    }
}

You could call deleteButtonShowHide function when you are loading/setting up a cell:您可以在加载/设置单元格时调用deleteButtonShowHide function:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "TeamsCell") as? TeamsCell {
        cell.updateCell(team: teams[indexPath.row])
        cell.deleteButtonShowHide()  // <-- HERE
        cell.teamsCellDelegate = self
        return cell
    }
    return UITableViewCell()
}

By the way, your cell should not contain such logic in the first place.顺便说一句,您的单元格首先不应该包含这样的逻辑。 It should depend on some data model object which then should be used to setup your cell correctly (show/hide UI elements, etc.).它应该取决于一些数据 model object 然后应该用于正确设置您的单元格(显示/隐藏 UI 元素等)。

You could simplify by setting the button show/hide when computing the number of row.您可以通过在计算行数时设置按钮显示/隐藏来简化。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if teams.count < 5 {plusBtn.isHidden = false}
    if teams.count == 4 { plusBtn.isHidden = true}
    return teams.count
}

And set the delebutton visibility when creating the cell:并在创建单元格时设置删除按钮可见性:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "TeamsCell") as? TeamsCell {
        cell.updateCell(team: teams[indexPath.row])
        // cell.teamsCellDelegate = self
        cell.deleteButton.isHidden = (teams.count < 3)
        return cell
    }
    return UITableViewCell()
}

So no need for delegate and cell does not have to know about the model (teams)因此无需代表和单元不必了解 model(团队)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM