[英]Manipulate uicollectionview inside uicollectionviewcell from uiviewcontroller
我有一個包含UICollectionView的UIViewController,並且在其UICollectionViewCell中嵌入了另一個UICollectionView 結構
而且在視覺上看起來是這樣的: 視覺
我想在UIViewController中按BOTON,然后從那里在外部UICollectionView的單元格內更改UICollectionView的backgroundColor
我的UIViewController類
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var scrollH: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
if let flowLayout = scrollH.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.minimumLineSpacing = 0
}
setupMenuQuestion()
}
var uiview: UIView = {
let view1 = UIView()
view1.backgroundColor = UIColor.brown
return view1
}()
var uibutton: UIButton = {
let btn1 = UIButton()
btn1.setTitle("BOTON", for: .normal)
btn1.addTarget(self, action: #selector(presionoBoton), for: .touchUpInside)
return btn1
}()
@objc func presionoBoton() {
delegateExterior?.cambiarBackground()
}
var delegateExterior: ExteriorCollectionViewCellDelegate?
private func setupMenuQuestion() {
view.addSubview(uiview)
uiview.translatesAutoresizingMaskIntoConstraints = false
uiview.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
uiview.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
uiview.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
uiview.heightAnchor.constraint(equalToConstant: 30).isActive = true
uibutton.translatesAutoresizingMaskIntoConstraints = false
uiview.addSubview(uibutton)
uibutton.centerYAnchor.constraint(equalTo: uiview.centerYAnchor).isActive = true
uibutton.centerXAnchor.constraint(equalTo: uiview.centerXAnchor).isActive = true
uibutton.leftAnchor.constraint(equalTo: uiview.leftAnchor, constant: 7).isActive = true
uibutton.rightAnchor.constraint(equalTo: uiview.rightAnchor, constant: -7).isActive = true
uibutton.heightAnchor.constraint(equalTo: uiview.heightAnchor, multiplier: 0.8).isActive = true
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cellNumQuestion = collectionView.dequeueReusableCell(withReuseIdentifier: "celdaExterior", for: indexPath) as! ExteriorCollectionViewCell
return cellNumQuestion
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height - 100)
}
}
我的UICollectionViewCell類
protocol ExteriorCollectionViewCellDelegate {
func cambiarBackground()
}
class ExteriorCollectionViewCell: UICollectionViewCell,
UICollectionViewDataSource, UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout, ExteriorCollectionViewCellDelegate{
func cambiarBackground() {
collectionCelda.backgroundColor = UIColor.black
}
@IBOutlet weak var collectionCelda: UICollectionView!
override init(frame: CGRect) {
super.init(frame: frame)
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 40
layout.minimumLineSpacing = 40
collectionCelda!.collectionViewLayout = layout
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell = UICollectionViewCell()
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "celdaInterior", for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: frame.width - 20, height: 400)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 30.0, left: 0.0, bottom: 30.0, right: 0.0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 40
}
}
我看過另一則與我的問題類似的帖子,但我不知道如何繼續
如何訪問嵌入在主UIViewController的父collectionViewCell中的collectionView?
在您的情況下,您未設置已創建的委托,應在內部設置它:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cellNumQuestion = collectionView.dequeueReusableCell(withReuseIdentifier: "celdaExterior", for: indexPath) as! ExteriorCollectionViewCell
// Setting the delegate
delegateExterior = cellNumQuestion
return cellNumQuestion
}
另外,為了避免引用循環,請將您的委托外部指針設置為弱:
weak var delegateExterior: ExteriorCollectionViewCellDelegate?
另一個解決方案是在ExteriorCollectionViewCell
添加一個觀察者 ,並在點擊按鈕並在UIViewController
內部調用IBAction方法時,使用后通知將該事件傳遞給觀察者。
即:
添加觀察者,該觀察者將偵聽您的ExteriorCollectionViewCell
的事件:
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "EventForCollectionView"),
object: nil,
queue: nil) { [weak self] (_) in
// This method will be invoked when you post notification from your IBAction method inside your UIVC
self?.YourCustomMethod()
}
通過IBAction方法中的IBAction發送通知事件:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "EventForCollectionView"), object: nil)
但是,如果按鈕在UICollectionViewCell
,則可以在UICollectionViewCell
內使用IBAction方法,並在那里進行邏輯處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.