ios - HeaderView 按钮以编程方式转到另一个 View Controller

标签 ios swift uitableview swift4 swift-protocols

我有一个带有按钮的原型(prototype)标题 View ,我希望能够单击该按钮以转到另一个 View Controller (TargetViewController)。

但是,由于某种原因,我无法让它工作。我正在尝试以编程方式完成这一切,因此没有像我通常习惯的 prepareForSegue 函数。你能告诉我我做错了什么吗?

如果使用委托(delegate),我还需要 addTarget 吗?我认为最大的问题是我不知道应该将哪个类设置为 delegate

我在这方面还很陌生,所以非常感谢任何帮助!

相关代码行如下:

protocol HeaderCellDelegate: class {
    func didTapEdit()
}

class HeaderCell: UITableViewHeaderFooterView {
    var editButton: UIButton!
    weak var delegate: HeaderCellDelegate?


    override init(reuseIdentifier: String?) {
        self.editButton = UIButton()
        self.editButton.setImage(UIImage(named: "Edit"), for: .normal)

        super.init(reuseIdentifier: reuseIdentifier)

        self.editButton.addTarget(self, action: #selector(editButtonPressed), for: .touchUpInside)

        self.contentView.addSubview(editButton)

        // Plaeholder for constraints
    }

    required init?(coder aDecoder: NSCoder) {
         fatalError()
    }

    @objc func editButtonPressed() {
        delegate?.didTapEdit()
        print("delegate didTapEdit")
    }
}

TableView Controller :

extension ProfileDataController: UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return tableViewSections.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableViewSections[section].rows.count
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {    
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderCell") as! HeaderCell
        return header
    }

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

目标 View Controller :

import UIKit

extension TargetViewController: HeaderCellDelegate {

    func didTapEdit() {
        print("editButtonPressed")
    }
}

最佳答案

如果没有委托(delegate),您可以在 ProfileDataController 中添加操作

extension ProfileDataController: UITableViewDataSource, UITableViewDelegate {
   func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderCell") as! HeaderCell
        header.editButton.addTarget(self, action: #selector(editButtonPressed), for: .touchUpInside)
        return header
   }
}

ProfileDataController

@objc func editButtonPressed() {
    print("edit button pressed")
    self.performSegue(withIdentifier: "YOUR SEGUE IDENTIFIRE", sender: nil)
}

关于ios - HeaderView 按钮以编程方式转到另一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48821225/

相关文章:

swift - 将字典用于表数据 Swift

swift - iOS 13 - 相机在 VNDocumentCameraViewController 中挂起

swift - ios13 UIPopoverViewController 显示 UITableViewController - 安全区域问题/表格缺失部分

ios - UIWebView的加载时间

ios - 带有导航栏大标题的 UITableView 奇怪的滚动行为,滚动到顶部时顶部的弹跳效果自动切断/生涩

ios - RxSwift 绑定(bind)到变量的格式化值

ios - 生成用于设置 Apple 推送通知的 .pem 文件

ios - 使用 google place api 添加价格级别很困难

ios - 导航栏标题 View 上的多行 UILabel

ios - 如何从 textFieldDidEndEditing 访问自定义 UITableViewCell?