Search code examples
swiftxcodeuibuttoncollectionview

UIButton in CollectionViewCell "IndexPath doesn't work"


I made a simple app add uibutton to collectionviewcell but when i give action an error occurs. Here is a screenshoot.

Error 1

Error 2

and my code of class: UIbuton {}

import Foundation
import UIKit

class KeypadUIButton: UIButton {
    var qIndex : NSIndexPath?
}

and my cell class is :

import UIKit

class ConverterCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var keypadButton: KeypadUIButton!
}

and error in :

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = padCollectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! ConverterCollectionViewCell
        let index = indexPath.row
       // -> cell.keypadButton.qIndex = indexPath as NSIndexPath
return cell
}

Where is my mistake. can you help me ?


Solution

  • Oh! I've got your error, it seems you missed this:

    Custom class name

    Type in your class name: KeypadUIButton

    Also you don't need to use NSIndexPath in swift. Just use IndexPath instead. So you won't need to down cast it.