从集合 1 更新集合中的单元格内容

Posted

技术标签:

【中文标题】从集合 1 更新集合中的单元格内容【英文标题】:Update cell content in UICollection2 from UICollection 1 【发布时间】:2018-12-04 04:24:55 【问题描述】:

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

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

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

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

第一个单元格(从主 uicollection 视图)包含一个带有 3 个单元格(红色)的 uicollection。当我单击(点击)位于红色 uicollection 中的任何(3 个)单元格时,我想更新蓝色 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 中的任何类似问题。如果是这样,请指出我正确的方向。

我真的希望我在这里解释了自己。感谢您的意见。

【问题讨论】:

代码太多!! @SPatel 我知道这是很多代码,但我试图尽可能具体。我不想让人们告诉我我缺少方法。 【参考方案1】:

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

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

当您设置主集合视图时,您将可以访问 RedCollectionView 和 BlueCollectionView.. 将委托设置为主 collectionView 或 BlueCollection 或两者.. 这完全取决于您。

然后定义函数并做任何你想做的事。

【讨论】:

以上是关于从集合 1 更新集合中的单元格内容的主要内容,如果未能解决你的问题,请参考以下文章

DataGridView控件中如何获取选中单元格内容?

发生单元格内容交互时更新 TableView 单元格高度

将数据从集合视图单元格内的表视图单元格传输到另一个集合视图单元格 [带图片!]

EXCEL中,如何引用一个单元格中的数据,作为另一个单元格内容中的一部分?

在表格视图中的所有单元格都已加载后,重新加载表格视图单元格内的集合视图

如何在表格视图单元格内创建集合视图