[英]How to display data from collectionview inside tableviewcell to another view controller when tableview header button is clicked
[英]how to get table view row and push to viewController from collectionView that's inside a tableViewCell when tapped button inside collectionView cell
我的層次結構像這樣 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 的視圖控制器)中,我設置如下:
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 的委托。 請按照以下步驟再次嘗試您的委托,也許您忘記在嵌套組件中進行設置....
//帶有委托函數的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 將自身設置為表格單元格中的委托...
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 設置的委托
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
class AccountTypeCollectionViewCell: UICollectionViewCell {
//......
//Multiple component
//......
weak var delegate: CustomCollectionCellDelegate?
@IBAction func btnMoreInfoPressed(_ sender: UIButton) {
print("btnMoreInfoPressed eh dipencet")
delegate?.OpenNows(wasPressedOnCell: self)
}
}
試試這個,讓你的代碼更簡單、更清晰,以簡化你未來的調整:) 告訴我它是否有效
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.