How to Make Prepareforsegue with Uicollectionview

How to make prepareForSegue with UIcollectionView

The following code should work well for you :

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "showDetailsSegue" {

let detailsVC = segue.destinationViewController as! DetailsViewController
let cell = sender as! StampsCollectionViewCell
let indexPaths = self.couponsCollectionView.indexPathForCell(cell)
var thisCoupon = self.userCoupons[indexPaths!.row] as UserCoupons
detailsVC.userCoupon = thisCoupon
detailsVC.pinArray = self.pinArray
detailsVC.userStamp = self.userStamps[indexPaths!.row]

}

}

Update : Please check the incoming data from the server if its correct or not and it might work !

How to prepareForSegue in a UICollectionView depending on the contents of cell?

I assume your actual question is whether the ideal method would be written in the performSegue method or in the didSelectItemAtIndexPath.

If this is the case, both can work.

You can try first didSelectItemAtIndexPath and pass the model object (in your case that could be Event) to the detail view controller. That could look like this:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
// Get the Event model representing the currently selected cell
Event *selectedEvent = self.events[indexPath.row];

// Create a new detail view controller with the selected event and push it
EventDetailViewController *eventDetailViewController = [[EventDetailViewController alloc] initWithEvent: selectedEvent];
[self.navigationController pushViewController:eventDetailViewController animated:YES];
}

If you want to use segues, your goal is again to get the selected indexPath. You can achieve that by using UICollectionView's indexPathForCell method as described in more detail in this answer.

Note that it might simplify your code a lot if you use a dedicated model class (the Event one I mentioned earlier) and then have an array of events as your datasource.

UICollectionView Getting indexPath in prepareForSegue

You can set sender as anything you want. This may help:

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView!.cellForItemAtIndexPath(indexPath) as! ImageCollectionViewCell
performSegue(withIdentifier: "collectionSegue", sender: cell)
}

Preparing segue in Collection View

I have mode some changes to your code

Add following changes in your code

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
self.performSegueWithIdentifier("showImage", sender: indexPath)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "showImage"
{
let indexPath : NSIndexPath = sender as! NSIndexPath
let vc = segue.destinationViewController as! NewViewController
vc.image = self.imageArray[indexPath.row]!
vc.title = self.appleProducts[indexPath.row]
}
}

prepareForSegue collectionView using swift

the problem is here:

func collectionView(collectionView: UICollectionView, selectedItemIndex: NSIndexPath) {
self.performSegueWithIdentifier(brandSegueIdentifier, sender: self)
}

the sender should be the cell but not the view controller itself, since you want to parse the sender to a cell in the prepareForSegue method, so it should be:

func collectionView(collectionView: UICollectionView, selectedItemIndex: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(selectedItemIndex);
self.performSegueWithIdentifier(brandSegueIdentifier, sender: cell);
}

Perform Segue from UICollectionViewCell

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)
}
}

Perform a segue from a UICollectionViewCell to show a detail screen not working

Change sender to the collectionView cell:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetail", sender: MovieCell)
}

Use this in prepare for segue:

if(segue.identifier == "showDetail") {
guard let viewController = segue.destination as? DetailsViewController else {return}
let cell = sender as MovieCell
guard let indexPath = self.collectionView!.indexPathForCell(cell) else {return}
viewController.movie = self.moviesList[indexPath.row]
}

how to pass data in perform segue sender through protocol & delegate from a UICollectionViewCell inside UITableViewCell?

When I started learning the delegation pattern I always made some mistakes. So I make a rule for myself. When you have to implement a delegate always remind 6 steps.

The First 3 steps are for the class that will pass the data or write protocol, here your AdminPList_TableViewCell is that class. The 3 steps are

  1. Write a protocol (a bunch of method declaration) // you did it.
  2. Declare a protocol variable inside the class that will pass the data. // you did that too (var delegate: MyCustomCellDelegator?)
  3. Call the methods you declared. // you did that (self.delegate?.cellWasPressed())

The Last 3 steps are for the class that will conform that protocol, here ProductsLibrary is that class.


  1. Conform that protocol in that class which will implement those methods. // here you did that (extension ProductsLibrary: ..., MyCustomCellDelegator)

  2. Assign delegate to self, but what is self here? well, self is the ProductsLibrary which has been delegated! // you missed that

  3. Implement the protocol methods. // you did that :)

How to solve this?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

...
cell.delegate = self // you missed this line
...
return cell
}

Now for passing selectedProduct just change the protocol method definition everywhere.

protocol MyCustomCellDelegator {
func cellWasPressed(withData: productObject)
}

Then call from the didSelectItemAt method

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

let selectedProduct = FP_Array[indexPath.row] // I want to pass it to sender in perform segue un ProductsLibrary Class
self.delegate?.cellWasPressed(withData: selectedProduct)

}

Now use it inside the method body.

func cellWasPressed(withData data: productObject) {
//now use the data and write your necessary code.
performSegue(withIdentifier: "EditProduct", sender: self)

}

Hope it will help you :).



Related Topics



Leave a reply



Submit