Greetings to all! I hope someone can solve my problem, I have my CollectionView, and in it I put a sampleObject, which contains an image and a label and a function to add these examples, this returns an array and I put it in my collection: and when I want to add a new object to said collection I do it from another ViewController, but I do not add anything to the collection I do not know what I'm doing wrong I'm new to this, someone mentioned that it depends on how the class where the collection collection object is instantiated, and which is it has to be initialized, but I have not found anything I put my code:
this is my object model:
class SampleObjects: NSObject {
var image: String?
var name: String?
init(name: String, image: String) {
self.image = name
self.name = image
super.init()
}
// funcion para crear los ejemplos de objetos
static func sampleNewObject() -> [SampleObjects] {
var objectos = [SampleObjetc]()
let objetoUno = SampleObjects(name: "nombre", Image: "miImagen")
objetos.append(objetoUno)
return objetos
}
I place the instance of the object in the viewController where I have my collectionView:
var misObjetos: [SampleObjects]?
I also create an instance of the object in the collectionViewCell to be able to place the data of the SampleObject and appear in the view and which are related to the properties within the collectionViewCell:
var objetosEnCollection: SampleObjetos? {
didSet {
if let name = propiedadEnCollection?.name {
nameImage.text = name
}
if let imageName = propiedadCollection?.image {
image.image = UIImage(named: imageName)
}
}
}
and I put it in the section in my collectionView:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == miCollectionView {
if let countObjetos = misObjetos?.count {
return countObjetos
}
return 0
}
and then in the collection cell:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == instrumentCarousel {
let cell: miColleccionCell = collectionView.dequeueReusableCell(withReuseIdentifier: carouselCellId, for: indexPath) as! miColleccionCell
if let celda = misObjetos?[indexPath.item] {
cell.misObjetos = celda
}
return cell
}
I also access the data of the model so that I can add the function and colóco in the ViewController where my collectionView is:
misObjetos = SampleObjects.sampleNewObject()
everything is fine here, it appears in the cell, if I add more objects to the array inside the function in SampleObjetc ... and it appears in the CollectionView ...
the detail is at this point ... I'm in another viewController, and I have a button, in which is going to add one more object in the array for the collectionView cell .. and I do the following ...
I urge the classes to call the collection where the new object will go
var misObjetos: [SampleObjects]?
var miColeccion: miCollectionView?
// no se si tenga que inicializar aqui esta declaración si es asi no se como hacerlo...
and in the function of the button I do the following:
@objc func addTrack() {
let objetosCollection = ViewController.miCollectionView
let new = SampleObjects.init(name: "nombre", image: "miImagen")
do {
misObjetos.append(new)
let item = misObjetos.count + 1
let insertOnCollection = IndexPath(item: item, section: 0)
objetosCollection?.insertItems(at: [insertOnCollection])
}
dismiss(animated: true, completion: nil)
}
and run the app, but when wanting to add the object does not put anything, maybe it is not the way to add an object to the collection, I've been looking for a solution and I have not found anything, I hope someone can remove this doubt. .. I use Swift 4, greetings to all