2017-05-24 19 views
0

간단한 응용 프로그램에서 collectionviewcell에 uibutton을 추가했지만 작업을 수행 할 때 오류가 발생합니다. 다음은 스크린 샷입니다.CollectionViewCell의 UIButton "IndexPath does not work"

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 

} 

}