Frank Boccia Frank Boccia - 5 months ago 21
iOS Question

How to make different cells in a collection view go to different view controllers in Swift

I am trying to make the different cells each be able to be clicked and bring them to a new view controller.

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UICollectionViewCell

let cellLabels = cell.viewWithTag(1) as! UILabel
cellLabels.text = arrayOfCellLabel[indexPath.row]
return cell

} // # Of Cells
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrayOfCellLabel.count
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0 {
self.performSegueWithIdentifier("box", sender: self)
}
if indexPath.row == 1 {
self.performSegueWithIdentifier("box1", sender: self)
}


}

Answer

You need to use Collection views delegate instead of Table view's delegate

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){

 if indexPath.row == 0  {
        self.performSegueWithIdentifier("box", sender: self)
    }
    if indexPath.row == 1  {
        self.performSegueWithIdentifier("box1", sender: self)
    }

}