0

I'm trying to get the indexPath.row of the clicked button for delete action and to perform a segue to the next controller for confirmation.

I have a collectionView with the list of students where there is a button in the cell where if i click on the button it should perform a segue to a controller to confirm the action. The issue now I cant seem to get the indexPath. I have done some of the research and similar posts that was found here. I managed to follow through till on step 6, when I tried to key in self.collectionView.indexPathForCell(cell). An error showed

Ambiguous reference to member 'collectionView(_:numberOfItemsInSection:)'

Delegate

protocol myCellDelegate: AnyObject {
    func deletePressed(cell: myCells)
}


class myCells : UICollectionViewCell{
    @IBOutlet weak var studentName: UILabel!
    @IBOutlet weak var deleteButton: UIButton!

    weak var delegate: myCellDelegate?


    @IBAction func deleteClicked(_ sender: Any) {
        delegate?.deletePressed(cell: self)
    }
}

ViewController

class RoomDetailViewController: UIViewController ,UICollectionViewDelegate,UICollectionViewDataSource,myCellDelegate {


    func deletePressed(cell: myCells) {
        let indexPath = self.collectionView.indexPathForCell(cell)
    }



    var selectedCell = 0


    var students = ["Brandon","Brenda","Louise","Angela","Arthur"]

    override func viewDidLoad() {
        super.viewDidLoad()
        customBackButton()
        // Do any additional setup after loading the view.
    }

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

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.students.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell", for: indexPath) as! myCells

        cell.studentName.text = students[indexPath.item]
        selectedCell = indexPath.row

        return cell
    }

}

I'm aware that there have been many post on this indexPath topic but none of them were able to solve my problem. Anyone know what went wrong?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Dexter Siah
  • 33
  • 1
  • 2
  • 9

1 Answers1

0

It's a collection view, not a table view. Use indexPath(for:), not indexPathForCell.

let indexPath = self.collectionView.indexPath(for: cell)

You also need to set the cell's delegate in cellForItemAt:

cell.delegate = self
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • It doesn't work either.... when i type collectionView. it doesnt appear any of the methods instead it is expectecting collectionView() – Dexter Siah Nov 08 '19 at 04:21
  • Make sure your view controller has a `collectionView` property for the collection view. – rmaddy Nov 08 '19 at 04:24
  • What do you mean have a `collectionView` property in the view controller? Adding `let collectionView = UICollectionView()` into my `RoomDetailViewController`? – Dexter Siah Nov 08 '19 at 04:31
  • Your view controller has a collection view in it already, right? Do you have some property currently that holds a reference to the UICollectionView? What did you name that property? Use that existing property instead of `collectionView`. – rmaddy Nov 08 '19 at 04:34
  • No I dont have any reference to the UICollectionView. The code above is all i have in the viewController and used the storyboard to dragged the collectionView so I didnt initialize any UICollectionView in my viewController – Dexter Siah Nov 08 '19 at 04:40
  • Then add an outlet for the collection view. Connect the existing collection view to the outlet. Name the outlet `collectionView`. Then the code in the answer should work. – rmaddy Nov 08 '19 at 04:41
  • Thank you sir that solved the problem however one more question, in my deletePressed function i tried printing out the indexPath?.row but nothing came out. Any ideas? – Dexter Siah Nov 08 '19 at 04:50
  • That means that `self.collectionView.indexPath(for: cell)` is not finding the cell. And that probably means you don't have things setup correctly. Make sure you are not creating and assigning a new collection view to your new `collectionView` outlet. Make sure you are just connecting the collection view from your storyboard to the outlet. – rmaddy Nov 08 '19 at 04:54
  • I've updated the post, in the deletePressed function i tried to print dummy data so to see if the function was running and when i tried pression on my button it doesn't fire deletePressed at all – Dexter Siah Nov 08 '19 at 05:07
  • See my updated answer. You forgot to set the cell's delegate. – rmaddy Nov 08 '19 at 05:10