2017-05-24 1 views
0

私はcollectionviewcellにuibuttonを追加する簡単なアプリを作ったが、私がアクションを与えるとエラーが発生する。ここにスクリーンショットがあります。CollectionViewCellのUIButton "IndexPathが機能しません"

Error 1

Error 2

クラスの私のコード:UIbuton {}

import Foundation 
import UIKit 

class KeypadUIButton: UIButton { 
    var qIndex : NSIndexPath? 
} 

と私のセルクラスがある:

import UIKit 

class ConverterCollectionViewCell: UICollectionViewCell { 

    @IBOutlet weak var keypadButton: KeypadUIButton! 
} 

とエラー:

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 
} 

私の間違いはどこですか?手伝って頂けますか ?

+0

あなたの 'keypadButton'コンセントは接続されていません。 – rmaddy

+0

あなたのstroyboardでは、ボタンクラスを 'KeypadUIButton'に設定しましたか? – Bilal

答えて

0

ああにカスタムクラス名だけでなく、あなたのボタンにアクションを与えることをその作業罰金が、確認してくださいあなたのコードを試してみます!あなたのクラス名で

Custom class name

タイプ:KeypadUIButton

また、あなたが迅速にNSIndexPathを使用する必要はありません。私はあなたがこれを逃したようで、自分のエラーを持っています。代わりにIndexPathを使用してください。だから、それをダウンキャストする必要はありません。

-1

あなたはよう

qIndexを取っていましたか? NSIndexPath

あなたにqIndex

として取ることができますか? Indexpath

cellforitemで不必要なダウンキャスティングを回避します。

0

私はあなたがあなたのボタンcustom button

class KeypadUIButton: UIButton { 
    var qIndex : NSIndexPath? 
} 

class ConverterCollectionViewCell: UICollectionViewCell { 

    @IBOutlet weak var keypadButton: KeypadUIButton! 
} 

class ViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from 
    a nib. 
} 

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


} 

extension ViewController : UICollectionViewDataSource{ 

func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 1 
} 
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 16 
} 

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

    let identifier : String = "test" 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! ConverterCollectionViewCell 
    cell.keypadButton.qIndex = indexPath as NSIndexPath? 
    cell.keypadButton.setTitle("\(indexPath.row + 1)", for: .normal) 

    return cell 

} 

} 
関連する問題