2016-08-19 2 views
2

Ich habe meine UICollectionView programmgesteuert erstellt und in diesem Fall meine didSelectItemAtIndexPath Methode überhaupt nicht aufrufen.Warum UICollectionView didSelect-Methode nicht funktioniert?

let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: CGRectGetWidth(self.view.frame), height: 360), collectionViewLayout: layout) 
collectionView.delegate = self 
collectionView.dataSource = self 
collectionView.userInteractionEnabled = true 

Also, was ist ein Problem? Warum, wenn ich auf die Zellen klopfe, bekomme ich meine Antwort nicht?

+0

Delegiertenobjekt nicht 'nil'? –

+0

1) Versuchen Sie 'Background Color' von' UICollectionView' und 'UICollectionViewCell' einzustellen. Sehen Sie, ob es auf dem Bildschirm richtig angezeigt wird oder nicht. 2) Setze 'userInteractionEnabled' = true für' UICollectionView' – pkc456

+0

@ pkc456 Warum? Ich sehe bereits meine Zellen und setze bereits userInteraction aktiviert – Doe

Antwort

0

Wählen Sie ViewController.swift im Projektnavigator. Aktualisieren Sie die Klassendeklaration mit diesem:

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource 


//After class declaration create a new variable: 

var collectionView: UICollectionView! 



    override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
    layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10) 
    layout.itemSize = CGSize(width: 90, height: 120) 

    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 
    collectionView.dataSource = self 
    collectionView.delegate = self 
    collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell") 
    collectionView.backgroundColor = UIColor.whiteColor() 
    self.view.addSubview(collectionView) 
} 



func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    print("Selected Cell: \(indexPath.row)") 
} 
Verwandte Themen