【发布时间】:2020-01-16 07:59:13
【问题描述】:
我的层次结构像这样 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 行。
【问题讨论】:
-
您应该将
UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout推送到TableViewController,不要在tableViewCell内处理它们,这样您就可以在没有其他代表的情况下轻松获取所选行,here 是一个例子 -
@Tj3n,问题是我的TableViewController是空的,在tableview里面,我叫AccountTypeTableViewCell xib,在AccountTypeTableViewCell里面我叫另一个xib(AccountTypeCell)
-
AccountTypeTableViewCell xib 包括 UITableViewCell -> UICollectionView。 AccountTypeCell xib 包括 UICollectionViewCell 及其内容
标签: ios swift uitableview uicollectionview