简体   繁体   English

当点击collectionView单元格内的按钮时,如何获取表格视图行并从位于tableViewCell内的collectionView推送到viewController

[英]how to get table view row and push to viewController from collectionView that's inside a tableViewCell when tapped button inside collectionView cell

My hierarchy like this UINavigationController->UITableviewController -> UITableViewCell -> UICollectionView-> UICollectionViewCell我的层次结构像这样 UINavigationController->UITableviewController -> UITableViewCell -> UICollectionView-> UICollectionViewCell

I have a button inside collectionView cell and I set like this:我在 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()
}}

In tableView cell, I set like this :在 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!
    }}

In AccountTypeViewController (view controller of tableview) I set like this :在 AccountTypeViewController(tableview 的视图控制器)中,我设置如下:

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

I tried with delegate2?.navigationController?.pushViewController(signUpViewController, animated: true) , it cannot push to signUpViewController.我试过delegate2?.navigationController?.pushViewController(signUpViewController, animated: true) ,它不能推送到 signUpViewController。

I also tried with delegate?.OpenNows(wasPressedOnCell: self) also cannot push to signUpViewController.我也尝试过使用delegate?.OpenNows(wasPressedOnCell: self)也无法推送到 signUpViewController。

And I want also get tableView row inside collectionView cell, how to do that?而且我还想在 collectionView 单元格中获取 tableView 行,该怎么做?

Please help me to correct my code so it can push to signUpViewController and get tableView row inside collectionView cell.请帮助我更正我的代码,以便它可以推送到 signUpViewController 并在 collectionView 单元格中获取 tableView 行。

You can simply pass the delegate that push to the next ViewController between all that mass.您可以简单地在所有质量之间传递推送到下一个 ViewController 的委托。 Follow this steps and try your delegate again, maybe you forgot to set in the nested components....请按照以下步骤再次尝试您的委托,也许您忘记在嵌套组件中进行设置....

//ViewControllerExtension with delegate functions //带有委托函数的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 which set itself as a delegate in the table cells... // ViewController 将自身设置为表格单元格中的委托...

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
     }

}

// Table cell which use the delegate set by the ViewController inside the nested CollectionCells // 表格单元格使用嵌套的 CollectionCells 中的 ViewController 设置的委托

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

// CollectionCell that trigger the delegate function nested between components // 触发嵌套在组件之间的委托函数的 CollectionCell

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

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

Try this and make your code easier and cleaner to simplify your future adjustment :) Tell me if it's work试试这个,让你的代码更简单、更清晰,以简化你未来的调整:) 告诉我它是否有效

暂无
暂无

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

相关问题 单击tableview header按钮时,如何将tableviewcell内的collectionview中的数据显示到另一个视图controller - How to display data from collectionview inside tableviewcell to another view controller when tableview header button is clicked 单击CollectionView单元格时如何更新表视图数据(tableview.reloadData()在CollectionView的didSelectItemAt内部不起作用) - How to update table view data when a CollectionView Cell is clicked ( tableview.reloadData() not working inside CollectionView's didSelectItemAt ) 正确地从 tableviewcell 内的 collectionview 转到另一个 viewcontroller - Properly segue from collectionview inside tableviewcell to another viewcontroller 如何重新加载tableviewcell内的collectionview - how to reload a collectionview that is inside a tableviewcell 如何从位于 tableView 单元格内的 collectionView 的 didSelectItemAt 导航到 viewController? - How can I Navigate to a viewController from a collectionView's didSelectItemAt which is inside a tableView cell? [以编程方式]选择嵌入在 collectionView 单元格中的 tableView 行时推送 viewController - [Programmatically]Push a viewController when selecting tableView row embedded in a collectionView cell CollectionView与TableViewCell中的图像 - CollectionView with images inside a TableViewCell TableViewCell 内的 CollectionView - CollectionView inside TableViewCell 如何在CollectionView内部实现ViewController? - How to implement ViewController with CollectionView inside? collectionView单元格中的按钮选择器 - selector for button inside collectionView cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM