ios - 当点击 collectionView 单元格内的按钮时,如何获取 TableView 行并从 tableViewCell 内的 collectionView 推送到 viewController

标签 ios swift uitableview uicollectionview

我的层次结构如下 UINavigationController->UITableviewController -> UITableViewCell -> UICollectionView-> UICollectionViewCell

我在 collectionView 单元格内有一个按钮,我设置如下:

class AccountTypeCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var AccountTypeView: UIView!
@IBOutlet weak var imgAccountType: UIImageView!
@IBOutlet weak var lblAccountTypeTitle: UILabel!
@IBOutlet weak var txtAccountTypeDescription: UITextView!
@IBOutlet weak var btnMoreInfo: UIButton!
@IBOutlet weak var btnOpenNow: UIButton!
weak var delegate: CustomCollectionCellDelegate?
weak var delegate2:AccountTypeViewController?
@IBAction func btnMoreInfoPressed(_ sender: UIButton) {
    print("btnMoreInfoPressed eh dipencet")
    delegate?.OpenNows(wasPressedOnCell: self)
}
@IBAction func btnOpenNowPressed(_ sender: RoundButton) {
    print("btnOpenNowPressed eh dipencet")
    let accountRegular = Account(accountType: "signUp.accountTypeRegular".localized(), code: AccountType.regular)
    let signUpViewController = SignUpViewController.fromStoryboard(name: AppStoryboard.Signup.instance)
    let signUpViewModel = SignupViewModel.shared
    signUpViewModel.accountType = accountRegular
    signUpViewController.signupViewModel = signUpViewModel
    delegate2?.navigationController?.pushViewController(signUpViewController, animated: true)
}

class var CustomCell : AccountTypeCollectionViewCell {
    let cell = Bundle.main.loadNibNamed("AccountTypeCell", owner: self, options: nil)?.last
    return cell as! AccountTypeCollectionViewCell
}

override func awakeFromNib() {
    super.awakeFromNib()
}}

在 tableView 单元格中,我设置如下:

protocol CustomCollectionCellDelegate:class {
func collectionView(collectioncell:AccountTypeCollectionViewCell?, didTappedInTableview TableCell:AccountTypeTableViewCell, collectionRow: Int)
func OpenNows(wasPressedOnCell cell: AccountTypeCollectionViewCell?)}

class AccountTypeTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    weak var cellDelegate:CustomCollectionCellDelegate?
    weak var myParent:AccountTypeViewController?
    @IBOutlet weak var collectionAccountTypeView: UIView!
    @IBOutlet weak var lblAccountTypeTitle: UILabel!
    @IBOutlet weak var collectionView: UICollectionView!
    let cellReuseId = "CollectionViewCell"
    class var customCell : AccountTypeTableViewCell {
        let cell = Bundle.main.loadNibNamed("AccountTypeTableViewCell", owner: self, options: nil)?.last
        return cell as! AccountTypeTableViewCell
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .horizontal
        flowLayout.itemSize = CGSize(width: 289, height: 289)
        flowLayout.minimumLineSpacing = 10.0
        self.collectionView.collectionViewLayout = flowLayout

        self.collectionView.dataSource = self
        self.collectionView.delegate = self

        let cellNib = UINib(nibName: "AccountTypeCell", bundle: nil)
        self.collectionView.register(cellNib, forCellWithReuseIdentifier: cellReuseId)
        collectionView.reloadData()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    var collectionViewOffset: CGFloat {
        get {
            return collectionView.contentOffset.x
        }

        set {
            collectionView.contentOffset.x = newValue
        }
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? AccountTypeCollectionViewCell
        self.cellDelegate?.collectionView(collectioncell: cell, didTappedInTableview: self, collectionRow: indexPath.row)
    }


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellReuseId, for: indexPath) as? AccountTypeCollectionViewCell
        cell?.updateCellWithImage(name: "videoCallWaiting")
        setRoundedViewWithBorder(view: (cell?.AccountTypeView)!, borderColor: "999999", bgColor: "FFFFFF")
        cell?.txtAccountTypeDescription.isScrollEnabled = true
        return cell!
    }}

在AccountTypeViewController(tableview的 View Controller )中我设置如下:

extension AccountTypeViewController:CustomCollectionCellDelegate {
    func OpenNows(wasPressedOnCell cell: AccountTypeCollectionViewCell?) {
        print("hasil nya eh dipencet")
        let accountRegular = Account(accountType: "signUp.accountTypeRegular".localized(), code: AccountType.regular)
        let signUpViewController = SignUpViewController.fromStoryboard(name: AppStoryboard.Signup.instance)
        let signUpViewModel = SignupViewModel.shared
        signUpViewModel.accountType = accountRegular
        signUpViewController.signupViewModel = signUpViewModel
        navigationController?.pushViewController(signUpViewController, animated: true)
    }

    func collectionView(collectioncell: AccountTypeCollectionViewCell?, didTappedInTableview TableCell: AccountTypeTableViewCell, collectionRow: Int) {
        print("collectionRow clicked:\(collectionRow)")
    }
}

我尝试使用delegate2?.navigationController?.pushViewController(signUpViewController,animated: true),它无法推送到signUpViewController。

我也尝试过使用delegate?.OpenNows(wasPressedOnCell: self)也无法推送到signUpViewController。

我还想在 collectionView 单元格中获取 tableView 行,该怎么做?

请帮助我更正我的代码,以便它可以推送到signUpViewController并在collectionView单元格内获取tableView行。

最佳答案

您可以简单地在所有这些质量之间传递推送到下一个 ViewController 的委托(delegate)。按照此步骤再次尝试您的委托(delegate),也许您忘记在嵌套组件中进行设置...

//带有委托(delegate)函数的ViewControllerExtension

extension AccountTypeViewController:CustomCollectionCellDelegate {
    func OpenNows(wasPressedOnCell cell: AccountTypeCollectionViewCell?) {
        print("hasil nya eh dipencet")
        let accountRegular = Account(accountType: "signUp.accountTypeRegular".localized(), code: AccountType.regular)
        let signUpViewController = SignUpViewController.fromStoryboard(name: AppStoryboard.Signup.instance)
        let signUpViewModel = SignupViewModel.shared
        signUpViewModel.accountType = accountRegular
        signUpViewController.signupViewModel = signUpViewModel
        navigationController?.pushViewController(signUpViewController, animated: true)
    }

    func collectionView(collectioncell: AccountTypeCollectionViewCell?, didTappedInTableview TableCell: AccountTypeTableViewCell, collectionRow: Int) {
        print("collectionRow clicked:\(collectionRow)")
    }
}

//ViewController 将自己设置为表格单元格中的委托(delegate)...

class AccountTypeViewController: UIViewController {
     //......
     //multiple code
     //......

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          //when you dequeue your cell insert the delegate
          // Everything I write next is pseudocode so do not simply copy+paste
          let cell = AccountTypeTableViewCell()
          cell.delegate = self
          return cell
     }

}

//使用嵌套 CollectionCells 内的 ViewController 设置的委托(delegate)的表格单元

class AccountTypeTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
     weak var cellDelegate: CustomCollectionCellDelegate?

     //......
     //multiple code
     //......

     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
           //when you dequeue your cell insert the delegate
           // Everything I write next is pseudocode so do not simply copy+paste
           let cell = AccountTypeCollectionViewCell()
           cell.delegate = cellDelegate
           return cell
     }
}

//触发组件之间嵌套的委托(delegate)函数的CollectionCell

class AccountTypeCollectionViewCell: UICollectionViewCell {
     //......
     //Multiple component
     //......
     weak var delegate: CustomCollectionCellDelegate?

     @IBAction func btnMoreInfoPressed(_ sender: UIButton) {
         print("btnMoreInfoPressed eh dipencet")
         delegate?.OpenNows(wasPressedOnCell: self)
     }
 }

试试这个,让你的代码更简单、更干净,以简化你 future 的调整:) 告诉我这是否有效

关于ios - 当点击 collectionView 单元格内的按钮时,如何获取 TableView 行并从 tableViewCell 内的 collectionView 推送到 viewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59765179/

相关文章:

iOS可变数组始终为空

ios - 从自定义 UITableViewCell 中的按钮执行 segue

ios - 如何在字符串数组中添加数字? swift

ios - Instagram/tags/\(hashtag)/media/recent 端点不返回分页?

jquery - Ajax 请求不适用于 iPad 1 (iOS 5.1.1)

ios - 在swift中设置模糊效果全屏

ios - 无法将内容附加到数组

swift - 从 Firebase 同步读取

ios - 将流式 (utf8) 数据转换为字符串的安全方法是什么?

ios - 滑动删除单元格导致 tableView 标题随单元格移动