Search code examples
iosswiftuicollectionviewuinavigationcontrolleruibutton

Question about UIButton behavior inside collectionView cell in Swift


I made the collectionView cell a dynamic type and put the information such as image, title, author and so on. This information is about the book, and we have created a "view" button at the bottom of the cell. When I press cell

if let navigationController = segue.destination as? UINavigationController,
                let index = collectionView.indexPathsForSelectedItems?.first,

                let readBookController = navigationController.viewControllers.first as? ReadBookViewController {

                readBookController.bookNo = self.booksCategory?.books[index.row].bookNo
                readBookController.bookTitle = self.booksCategory?.books[index.row].bookTitle
            }

This code works fine.

But when I press the button inside the cell, the behavior when the cell is pressed (let index = collectionView.indexPathsForSelectedItems? .First) => This code does not run, it just moves the VC.

Is there a way to pass the same data to VC when the cell is pressed and when the button is pressed in the cell?

code:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        switch (segue.identifier!) {
        case "TappedCellSegue":
            if let navigationController = segue.destination as? UINavigationController,
                let index = collectionView.indexPathsForSelectedItems?.first,

                let readBookController = navigationController.viewControllers.first as? ReadBookViewController {

                readBookController.bookNo = self.booksCategory?.books[index.row].bookNo
                readBookController.bookTitle = self.booksCategory?.books[index.row].bookTitle
            }
        case "TappedBtnSegue":    // Not working
            let readVC = self.storyboard?.instantiateViewController(withIdentifier: "ReadBookViewController") as! ReadBookViewController

            if let navigationController = segue.destination as? UINavigationController,
                let index = collectionView.indexPathsForSelectedItems?.first,

                let readBookController = navigationController.viewControllers.first as? ReadBookViewController {
                readBookController.bookNo = self.booksCategory?.books[index.row].bookNo
                readBookController.bookTitle = self.booksCategory?.books[index.row].bookTitle
            }

            self.present(readVC, animated:true, completion: nil)
        default:
            break
        }
    }

Solution

  • In cellForItemAt you can addTarget for button and give indexPath.row:

    cell.Button.tag = indexPath.row cell.Button.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside)

    In buttonAction function:

    @objc func buttonAction(_ sender: UIButton) {
        let index = sender.tag
    
        if let readBookController = navigationController.viewControllers.first as? ReadBookViewController {
            readBookController.bookNo = self.booksCategory?.books[index].bookNo
            readBookController.bookTitle = self.booksCategory?.books[index].bookTitle
    
            self.present(readBookController, animated: true, completion: nil)
    }