Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift UICollectionViewDelegate方法未在UICollectionViewController子类中调用_Swift_Uicollectionview - Fatal编程技术网

Swift UICollectionViewDelegate方法未在UICollectionViewController子类中调用

Swift UICollectionViewDelegate方法未在UICollectionViewController子类中调用,swift,uicollectionview,Swift,Uicollectionview,我对UICollectionViewController进行了子类化,调用了UICollectionView的所有数据源方法,但没有调用UICollectionViewDelegate方法。原因可能是什么?下面是UICollectionViewController子类的实现 class CollectionCollectionViewController: UICollectionViewController { let carImages = ["mini_small","rover

我对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
}
试试这个

self.collectionView.delegate = self
希望这有帮助