如何在嵌套的集合视图单元格中点击按钮来推动视图控制器



在表视图中有一个集合视图,在集合视图单元格中有一个按钮。当这个按钮被按下时,我想按另一个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")
}
}

首先你必须在tableViewCell中创建一个属性:

weak var parent:ViewController?

然后在viewController中你必须使用cell作为tableView的row:

cell.parent = self

然后在collectionViewCell中创建相同的属性:

weak var parent:ViewController?

和使用collectionView函数单元格项:

cell.parent = parent

并在你的按钮函数中使用父元素:

@objc func onTap() {
let destinationVC = NextViewController()
parent?.navigationController.pushViewController(destinationVC,animated:true)
}

由于委托在collectionView中,你可以在TableView单元格中获得回调,然后通过另一个委托,你可以在ViewController中获得回调。

或者你也可以使用通知来获得回调。

也可以使用闭包。

最新更新