3

So I'm creating a blog app, and on the home news feed collection view (imageCollection, loaded from firebase database) I have a button. This button title depends on the Category of the image. What i'm having an issue with is performing the segue in the UICollectionViewCell class. I ran the button action with the print statement, and it worked. But when i try to add performSegue, well it doesn't let me. (Use of unresolved identifier 'performSegue')

Any tips? thank you!

P.S. i'm still fairly new to swift, so if i come off a little ignorant, i apologize

My ViewController

import UIKit
import Firebase
import FirebaseDatabase
import SDWebImage

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {



@IBOutlet weak var popImageCollection: UICollectionView!
@IBOutlet weak var imageCollection: UICollectionView!


var customImageFlowLayout = CustomImageFlowLayout()
var popImageFlowLayout = PopImagesFlowLayout()
var images = [BlogInsta]()
var popImageArray = [UIImage]()
var homePageTextArray = [NewsTextModel]()

var dbRef: DatabaseReference!
var dbPopularRef: DatabaseReference!

override func viewDidLoad() {
    super.viewDidLoad()
    dbRef = Database.database().reference().child("images")
    dbPopularRef = Database.database().reference().child("popular")
    loadDB()
    loadImages()
    loadText()
    customImageFlowLayout = CustomImageFlowLayout()
    popImageFlowLayout = PopImagesFlowLayout()

    imageCollection.backgroundColor = .white
    popImageCollection.backgroundColor = .white


    // Do any additional setup after loading the view, typically from a nib.
}
func loadText() {
    dbRef.observe(DataEventType.value, with: { (snapshot) in
        if snapshot.childrenCount > 0 {
            self.homePageTextArray.removeAll()
            for homeText in snapshot.children.allObjects as! [DataSnapshot] {
                let homeTextObject = homeText.value as? [String: AnyObject]
                let titleHome = homeTextObject?["title"]
                let categoryButtonText = homeTextObject?["category"]
                self.imageCollection.reloadData()
                let homeLabels = NewsTextModel(title: titleHome as! String?, buttonText: categoryButtonText as! String?)
                self.homePageTextArray.append(homeLabels)
            }
        }
    })
}
func loadImages() {
    popImageArray.append(UIImage(named: "2")!)
    popImageArray.append(UIImage(named: "3")!)
    popImageArray.append(UIImage(named: "4")!)

    self.popImageCollection.reloadData()
}
func loadDB() {
    dbRef.observe(DataEventType.value, with: { (snapshot) in
        var newImages = [BlogInsta]()
        for BlogInstaSnapshot in snapshot.children {
            let blogInstaObject = BlogInsta(snapshot: BlogInstaSnapshot as! DataSnapshot)
            newImages.append(blogInstaObject)
}
self.images = newImages
self.imageCollection.reloadData()
})

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView == self.imageCollection {
       return images.count
    } else {
        return popImageArray.count
    }

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView == self.imageCollection {
   let cell = imageCollection.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ImageCollectionViewCell
    let image = images[indexPath.row]
        let text: NewsTextModel
        text = homePageTextArray[indexPath.row]
        cell.categoryButton.setTitle(text.buttonText, for: .normal)
        cell.newTitleLabel.text = text.title
    cell.imageView.sd_setImage(with: URL(string: image.url), placeholderImage: UIImage(named: "1"))
        return cell
    } else {
       let cellB = popImageCollection.dequeueReusableCell(withReuseIdentifier: "popCell", for: indexPath) as! PopularCollectionViewCell
        let popPhotos = popImageArray[indexPath.row]
        cellB.popularImageView.image = popPhotos
        cellB.popularImageView.frame.size.width = view.frame.size.width
            return cellB
    }

}


}

My ImageCollectionViewCell.swift

import UIKit
import Foundation

class ImageCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var categoryButton: UIButton!

@IBOutlet weak var newTitleLabel: UILabel!

@IBOutlet weak var imageView: UIImageView!


@IBAction func categoryButtonAction(_ sender: Any) {
    if categoryButton.currentTitle == "Fashion" {
        print("Fashion Button Clicked")
        performSegue(withIdentifier: "homeToFashion", sender: self)
    }

}

override func prepareForReuse() {
    super.prepareForReuse()
    self.imageView.image = nil
    }

}
Adrian Navarro
  • 135
  • 2
  • 10

2 Answers2

4

You need a custom delegate. Do this:

protocol MyCellDelegate {
    func cellWasPressed()
}

// Your cell
class ImageCollectionViewCell: UICollectionViewCell {
    var delegate: MyCellDelegate?

    @IBAction func categoryButtonAction(_ sender: Any) {
        if categoryButton.currentTitle == "Fashion" {
            print("Fashion Button Clicked")
            self.delegate?.cellWasPressed()
        }
    }

}

// Your viewcontroller must conform to the delegate
class ViewController: MyCellDelegate {
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView == self.imageCollection {
        let cell = imageCollection.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ImageCollectionViewCell

        // set the delegate
        cell.delegate = self
      // ...... rest of your cellForRowAtIndexPath
    }


// Still in your VC, implement the delegate method
    func cellWasPressed() {
       performSegue(withIdentifier: "homeToFashion", sender: self)
    }
}
jjjjjjjj
  • 4,203
  • 11
  • 53
  • 72
0

You should use your own delegate. It is already described here

performSegue(withIdentifier:sender:) won't work from cell because it is UIViewController metod.

also you can make use of closure

gorzki
  • 433
  • 3
  • 10