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

旧街凉风 提交于 2021-01-29 14:13:06

问题


My hierarchy like this UINavigationController->UITableviewController -> UITableViewCell -> UICollectionView-> UICollectionViewCell

I have a button inside collectionView cell and I set like this:

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 :

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 :

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.

I also tried with delegate?.OpenNows(wasPressedOnCell: self) also cannot push to signUpViewController.

And I want also get tableView row inside collectionView cell, how to do that?

Please help me to correct my code so it can push to signUpViewController and get tableView row inside collectionView cell.


回答1:


You can simply pass the delegate that push to the next ViewController between all that mass. Follow this steps and try your delegate again, maybe you forgot to set in the nested components....

//ViewControllerExtension with delegate functions

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

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

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

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



来源:https://stackoverflow.com/questions/59765179/how-to-get-table-view-row-and-push-to-viewcontroller-from-collectionview-thats

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!