在UICollectionViewController子类中未调用UICollectionViewDelegate方法

时间:2017-11-22 06:05:33

标签: swift uicollectionview

Delegate and datasource are also set我已经将UICollectionViewController子类化,UICollectionView的所有数据源方法都被调用,但UICollectionViewDelegate方法没有被调用。可能是什么原因。 ?下面是UICollectionViewController子类的实现。

class CollectionCollectionViewController: UICollectionViewController {

    let carImages = ["mini_small","rover_small","smart_small","highlander_small","venza_small","volvo_small","vw_small","nissan_small","honda_small","jeep_small"]

override func viewDidLoad() {
    super.viewDidLoad()
    self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}

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

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

// MARK: UICollectionViewDataSource

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return carImages.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! MyCollectionViewCell
   let path =  Bundle.main.url(forResource: carImages[indexPath.row], withExtension: "jpg")
   let data = try! Data(contentsOf: path!)
   cell.imgView.image = UIImage(data: data)
    return cell
}

// MARK: UICollectionViewDelegate


// Uncomment this method to specify if the specified item should be highlighted during tracking
override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
    return true
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let path =  Bundle.main.url(forResource: carImages[indexPath.row], withExtension: "jpg")
    let data = try! Data(contentsOf: path!)
    let image = UIImage(data: data)
    return image!.size
}

1 个答案:

答案 0 :(得分:2)

试试这个

self.collectionView.delegate = self

希望这个帮助