我在表視圖中有一個集合視圖,在集合視圖單元格中有一個按鈕。當該按鈕被按下時,我想推另一個 vc。
我嘗試在集合視圖單元格中創建一個委托,但是集合視圖單元格在表格單元格中有一個 cellforitem 方法,因此無法在主視圖控制器中宣告該委托。
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
setUpTableView()
}
func setUpTableView() {
let tableView = UITableView()
view.addSubview(tableView)
tableView.register(
HomeRecommendStoreTableViewCell.self,
forCellReuseIdentifier: HomeRecommendStoreTableViewCell.identifier
)
tableView.register(
UITableViewCell.self,
forCellReuseIdentifier: "cell"
)
tableView.delegate = self
tableView.dataSource = self
tableView.backgroundColor = .black
tableView.showsVerticalScrollIndicator = false
tableView.separatorStyle = .none
tableView.contentInsetAdjustmentBehavior = .never
self.tableView = tableView
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: HomePopularTableViewCell.identifier, for: indexPath) as? HomePopularTableViewCell else { return tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)}
cell.selectionStyle = .none
return cell
}
}
class TableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
static let identifier = "HomeRecommendStoreTableViewCell"
private var collectionView: UICollectionView!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.backgroundColor = .black
setUpCollectionView()
}
required init?(coder: NSCoder) {
fatalError()
}
func setUpCollectionView() {
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(
frame: .zero,
collectionViewLayout: layout
)
layout.scrollDirection = .horizontal
collectionView.register(
HomeRecommendStoreCollectionViewCell.self,
forCellWithReuseIdentifier: HomeRecommendStoreCollectionViewCell.identifier
)
collectionView.register(
UICollectionViewCell.self,
forCellWithReuseIdentifier: "cell"
)
collectionView.clipsToBounds = false
collectionView.backgroundColor = .black
collectionView.showsHorizontalScrollIndicator = false
collectionView.delegate = self
collectionView.dataSource = self
contentView.addSubview(collectionView)
collectionView.snp.makeConstraints { make in
make.top.equalToSuperview().offset(40)
make.leading.equalToSuperview().offset(20)
make.trailing.equalToSuperview()
make.bottom.equalToSuperview().inset(80)
}
self.collectionView = collectionView
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: HomeRecommendStoreCollectionViewCell.identifier, for: indexPath
) as? HomeRecommendStoreCollectionViewCell else {
return collectionView.dequeueReusableCell(
withReuseIdentifier: "cell", for: indexPath
)
}
return cell
}
}
class CollectionViewCell: UICollectionViewCell {
static let identifier = "HomeRecommendStoreCollectionViewCell"
private lazy var listButton: UIButton = {
let button = UIButton()
return button
}()
override init(frame: CGRect) {
super.init(frame: frame)
setUp()
}
func setUp() {
listButton.addTarget(self, action: #selector(onTap), for: .touchUpInside)
}
@objc func onTap() {
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
uj5u.com熱心網友回復:
首先你必須在 tableViewCell 中創建一個屬性:
weak var parent:ViewController?
然后在 viewController 中,您必須將單元格用于 tableView 行:
cell.parent = self
然后在 collectionViewCell 中創建相同的屬性:
weak var parent:ViewController?
并將 collectionView func 單元用于專案:
cell.parent = parent
并在您的按鈕功能中使用該父級:
@objc func onTap() {
let destinationVC = NextViewController()
parent?.navigationController.pushViewController(destinationVC,animated:true)
}
uj5u.com熱心網友回復:
由于委托在 collectionView 內,您可以在 TableView 單元格中獲得回呼,然后使用另一個委托,您可以在 ViewController 中獲得回呼。
或者,您也可以使用通知來獲取回呼。
或者也可以使用閉包。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/337255.html
下一篇:如何快速決議不同結構的請求
