从UICollection 1更新UICollection2中的单元格内容

时间:2018-12-04 04:24:55

标签: ios swift uicollectionview uicollectionviewcell uicollectionreusableview

我有3个UICollection。主要的UICollection包含2个单元格,每个单元格包含一个UI集合。

在第一个单元格中,从主UICollection加载包含3个单元格的垂直集合。

在第二个单元格中,从主UICollection创建一个单元格uicollection视图。

enter image description here

请参见上图:主UICollectionView用黄色表示,每个2个单元格分别用红色和蓝色表示。

第一个单元格(从主uicollection视图中)包含具有3个单元格(红色)的uicollection。当我点击位于红色uicollection中的任何(三个)单元格时,我想更新蓝色uicollection视图单元格。

FullScreenImage.swift

class FullScreenImage: UIViewController, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource{



let bottomUIColletion: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.minimumLineSpacing = 0
    layout.minimumInteritemSpacing = 0
    layout.scrollDirection = .horizontal
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    return cv
}()


let bottomContentCell = "buttonContentCell"
let bottomButtonsCell = "buttonButtonsCell"



override func viewDidLoad() {
    super.viewDidLoad()
    self.bottomUIColletion.delegate = self
    self.bottomUIColletion.dataSource = self
    self.bottomUIColletion.register(BottomButtonsCollectionViewCell.self, forCellWithReuseIdentifier: bottomButtonsCell)
    self.bottomUIColletion.register(BottomContentCollectionViewCell.self, forCellWithReuseIdentifier: bottomContentCell)

    self.bottomUIColletion.translatesAutoresizingMaskIntoConstraints = false

    self.view.addSubview(self.bottomUIColletion)

    DispatchQueue.main.async {

        self.bottomUIColletion.topAnchor.constraint(equalTo: self.bottomView.topAnchor, constant: 0).isActive = true
        self.bottomUIColletion.leadingAnchor.constraint(equalTo: self.bottomView.leadingAnchor, constant: 0).isActive = true
        self.bottomUIColletion.trailingAnchor.constraint(equalTo: self.bottomView.trailingAnchor, constant: 0).isActive = true
        self.bottomUIColletion.heightAnchor.constraint(equalToConstant: self.bottomView.frame.height).isActive = true


    }


}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return 2
}



func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if indexPath.row == 0{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: bottomButtonsCell, for: indexPath) as! BottomButtonsCollectionViewCell
        return cell
    }

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: bottomContentCell, for: indexPath) as! BottomContentCollectionViewCell

    return cell

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if indexPath.row == 0 {
        return CGSize(width: 50, height: self.bottomUIColletion.frame.height)
    }

    return CGSize(width: self.bottomUIColletion.frame.width-50, height: self.bottomUIColletion.frame.height)
}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}




}

BottomButtonsCollectionViewCell.swift

import UIKit
import FontAwesome_swift

class BottomButtonsCollectionViewCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

override init(frame: CGRect) {
    super.init(frame: frame)
    setup()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

let parentCollection = FullScreenImage()
let otherController = BottomContentCollectionViewCell()

let buttonCollection: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.minimumLineSpacing = 0
    layout.minimumInteritemSpacing = 0
    layout.scrollDirection = .vertical
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.backgroundColor = UIColor.clear
    return cv
}()

let buttonCell = "buttonCell"

func setup(){

    buttonCollection.delegate = self
    buttonCollection.dataSource = self

    buttonCollection.register(ButtonLayout.self, forCellWithReuseIdentifier: buttonCell)

    buttonCollection.translatesAutoresizingMaskIntoConstraints = false

    addSubview(buttonCollection)

    buttonCollection.leadingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
    buttonCollection.trailingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
    buttonCollection.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
    buttonCollection.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true


 }

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 3
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: buttonCell, for: indexPath) as! ButtonLayout
    if indexPath.row == 0{
        cell.actionBtn.setImage(UIImage(named: "font_size_icon_white"), for: .normal)
    }else if indexPath.row == 1{
        cell.actionBtn.titleLabel?.font = UIFont.fontAwesome(ofSize: 25, style: .solid )
        cell.actionBtn.setTitle(String.fontAwesomeIcon(name: .font), for: .normal)
    }else if indexPath.row == 2{
        cell.actionBtn.titleLabel?.font = UIFont.fontAwesome(ofSize: 25, style: .solid )
        cell.actionBtn.setTitle(String.fontAwesomeIcon(name: .palette), for: .normal)
    }

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: self.frame.width, height: self.frame.height / 3)
}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {



    otherController.btnSelected = indexPath.row
        parentCollection.bottomUIColletion.reloadItems(at: parentCollection.bottomUIColletion.indexPathsForVisibleItems)




    parentCollection.bottomUIColletion.reloadData()
    parentCollection.bottomUIColletion.layoutIfNeeded()
    //parentCollection.bottomUIColletion.





}

}

class ButtonLayout: UICollectionViewCell{
override init(frame: CGRect) {
    super.init(frame:frame)
    setupCell()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

let actionBtn = UIButton()


func setupCell(){
    backgroundColor = UIColor.darkGray
    actionBtn.translatesAutoresizingMaskIntoConstraints = false
    addSubview(actionBtn)

    actionBtn.leadingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
    actionBtn.trailingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
    actionBtn.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
    actionBtn.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true

 }

}

BottomContentCollectionViewCell.swift

import UIKit

class BottomContentCollectionViewCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{

var btnSelected = Int()

let contentCollection: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.minimumLineSpacing = 0
    layout.minimumInteritemSpacing = 0
    layout.scrollDirection = .vertical
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.backgroundColor = UIColor.clear
    return cv
}()

let textCell = "textSizeCell"
let fontTypeCell = "fontCell"
let backgroundColorCell = "backgroundCell"
let defaultCellLayout = "defaultCellLayout"


override init(frame: CGRect) {
    super.init(frame: frame)
    setup()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}



func setup(){

    contentCollection.delegate = self
    contentCollection.dataSource = self

    contentCollection.register(textSizeCell.self, forCellWithReuseIdentifier: textCell)
    contentCollection.register(fontCell.self, forCellWithReuseIdentifier: fontTypeCell)
    contentCollection.register(BackgroundContentCollectionViewCell.self, forCellWithReuseIdentifier: backgroundColorCell)
    contentCollection.register(defaultCell.self, forCellWithReuseIdentifier: defaultCellLayout)

    contentCollection.translatesAutoresizingMaskIntoConstraints = false

    addSubview(contentCollection)

    contentCollection.leadingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
    contentCollection.trailingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
    contentCollection.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
    contentCollection.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true

}


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    print("Button Content Collection: \(btnSelected)")

    //var cell = UICollectionViewCell()

    switch btnSelected {
    case 0:
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: textCell, for: indexPath) as! textSizeCell
        print("I was called: textSizeCell viewcell")
        return cell

    case 1:
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: fontTypeCell, for: indexPath) as! fontCell
        cell.nameLabel.text = "Awe"
        print("I was called: fontcell viewcell")
        return cell

    case 2:
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: backgroundColorCell, for: indexPath) as! BackgroundContentCollectionViewCell
        print("I was called: backgroundCell viewcell")
        return cell

    default:
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: defaultCellLayout, for: indexPath) as! defaultCell
        print("I was called: defaultCell viewcell")
        return cell
    }


}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: self.frame.width, height: self.frame.height)
}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}


}

class textSizeCell: UICollectionViewCell{
override init(frame: CGRect) {
    super.init(frame:frame)
    setupCell()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func prepareForReuse() {
    super.prepareForReuse()
}

func setupCell(){
    backgroundColor = UIColor.purple
     print("textSizeCell called.")
}
}

class fontCell: UICollectionViewCell{
override init(frame: CGRect) {
    super.init(frame:frame)
    setupCell()
}

override func prepareForReuse() {
    super.prepareForReuse()

}

let nameLabel: UILabel = {
    let label  = UILabel()
    label.textAlignment = .center
    //label.adjustsFontSizeToFitWidth = true
    //label.minimumScaleFactor = 0.5
    label.font = label.font.withSize(10)
    label.textColor = UIColor.lightGray
    label.numberOfLines = 0
    label.translatesAutoresizingMaskIntoConstraints = false
    return label
}()

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}



func setupCell(){
    self.backgroundColor = UIColor.yellow
    addSubview(nameLabel)
    nameLabel.centerXAnchor.constraint(equalTo: self.safeAreaLayoutGuide.centerXAnchor, constant: 0).isActive = true
    nameLabel.centerYAnchor.constraint(equalTo: self.safeAreaLayoutGuide.centerYAnchor, constant: 0).isActive = true
    nameLabel.heightAnchor.constraint(equalToConstant: 40)
    print("Font Cell setupCell called.")

}
}



class defaultCell: UICollectionViewCell{
override init(frame: CGRect) {
    super.init(frame:frame)
    setupCell()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func prepareForReuse() {
    super.prepareForReuse()
}

func setupCell(){
    backgroundColor = UIColor.white


}
}

我一直试图根据didSelectItemAtBottomButtonsCollectionViewCell.swift中选择的单元格重新加载单元格内容,然后重新加载数据,但是我没有运气。运行应用程序时,不会重新加载。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {



    otherController.btnSelected = indexPath.row
        parentCollection.bottomUIColletion.reloadItems(at: parentCollection.bottomUIColletion.indexPathsForVisibleItems)




    parentCollection.bottomUIColletion.reloadData()
    parentCollection.bottomUIColletion.layoutIfNeeded()
    //parentCollection.bottomUIColletion.





}

我错过了什么吗?我已经为此工作了2天,我感到不知所措,有点沮丧。另外,我可能忽略了SO中的任何类似问题。如果是这样,请指出正确的方向。

我真的希望我在这里解释自己。谢谢您的投入。

1 个答案:

答案 0 :(得分:1)

您创建一个名为RedCollectionViewDelegate的委托,该委托将定义didSelect(cell)函数。

在您的RedCollectionView中定义变量weak var delegate : RedCollectionViewDelegate

设置主集合视图时,您将可以同时访问RedCollectionView和BlueCollectionView。使委托成为主collectionView或BlueCollection或两者都可以。完全由您决定。

然后定义函数并执行所需的任何操作。