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?