can't complete my collection View

Hi so i struggle with my collection View i try to follow this tuto : https://zeyad-elassal.medium.com/make-automatic-image-slider-in-ios-770ea1c28109 "canno't make a link because this URL is not a permit domain on this forum sorry"

and in the extension i have a bug appear

extension HomeClientViewController: UICollectionViewDataSource, UICollectionViewDelegate {
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    var cell = SliderHomeClient.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? SliderCollectionViewCellHomeClient
    cell?.imageSliderHomeClient.image = sliderImage[indexPath.row]
    return cell ?? <#default value#>
  }
   
  func collectionView(_ collectionView:UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return sliderImage.count
  }
}

i have one error appear :

The "imageSliderHomeClient" outlet from the HomeClient to the UIView is invalid. Outlets cannot be connected to repeating content.

but i have also see the collection View change here in the tuto was :

CollectionView -> CollectionViewCell -> imageView

And mine was CollectionView -> CollecitonViewCell -> ContentView -> imageView

so i try to delete the image View and rename my contentView as the same as my imageView and remove image.

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    var cell = SliderHomeClient.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? SliderCollectionViewCellHomeClient
//HERE is different
    cell?.imageSliderHomeClient = sliderImage[indexPath.row]
//HERE is different
    return cell ?? <#default value#>
  }
   
  func collectionView(_ collectionView:UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return sliderImage.count
  }
}

but i have another error appear :

Cannot assign value of type 'UIImage?' to type 'UIView?'

how can i fix these to make my collectionView plz

i put here my whole code if :

import UIKit
import Alamofire
import SwiftUI

class HomeClientViewController: UIViewController {   
  @IBOutlet weak var dotHomeClient: UIPageControl!
  @IBOutlet weak var SliderHomeClient: UICollectionView!

   var email: String?
   
  let sliderImage = [UIImage(named: "slide1"),
            UIImage(named: "slide2"),
            UIImage(named: "slide3"),
            UIImage(named: "slide4")]
   
  var timer = Timer()
  var counter = 0
   
  override func viewDidLoad() {
     
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    sliderHomeClient()
  }

   func sliderHomeClient(){
    dotHomeClient.numberOfPages = sliderImage.count
    dotHomeClient.currentPage = 0
    DispatchQueue.main.async {
       self.timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.changeImage), userInfo: nil, repeats: true)
      }
     
  }
   
  @objc func changeImage() {
       
     if counter < sliderImage.count {
       let index = IndexPath.init(item: counter, section: 0)
       self.SliderHomeClient.scrollToItem(at: index, at: .centeredHorizontally, animated: true)
       dotHomeClient.currentPage = counter
       counter += 1
     } else {
       counter = 0
       let index = IndexPath.init(item: counter, section: 0)
       self.SliderHomeClient.scrollToItem(at: index, at: .centeredHorizontally, animated: false)
        dotHomeClient.currentPage = counter
        counter = 1
      }
   }
   
}

extension HomeClientViewController: UICollectionViewDataSource, UICollectionViewDelegate {
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    var cell = SliderHomeClient.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? SliderCollectionViewCellHomeClient
    cell?.imageSliderHomeClient = sliderImage[indexPath.row]
    return cell ?? <#default value#>
  }
   
  func collectionView(_ collectionView:UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return sliderImage.count
  }

//different class 
class SliderCollectionViewCellHomeClient: UICollectionViewCell {
  @IBOutlet weak var imageSliderHomeClient2: UIImageView!
  @IBOutlet weak var imageSliderHomeClient: UIView!
}
Answered by Claude31 in 736684022

you need to return a cell, not an optional.

Just change as follows:

    return cell ?? SliderCollectionViewCellHomeClient()
Accepted Answer

you need to return a cell, not an optional.

Just change as follows:

    return cell ?? SliderCollectionViewCellHomeClient()

You have to connect the UIImageView to an IBOutlet in SliderCollectionViewCellHomeClient, not in HomeClientViewController.

The reason is:

  • if you try to connect in the VC code, you don't know which cell you reference
  • on the other hand, when you connect to the cell class, you connect to a specific cell instance. Of course you need to access image from cell with something like cell.image

Are you sure you connected the UIImageView in storyboard to the IBOutlet in the cell ? And there is not a remaining connection to some IBOutlet in the VC ?

  • Check by showing connections in storyboard.
  • Remove any inappropriate one

In ant case, do a Clean Build Folder

here is my storyboard and i can't delete Content View if i want to delete my Content View i need to destroy my cell "SliderCollectionViewCellHomeClient"

here is the the error :

//different class 
class SliderCollectionViewCellHomeClient: UICollectionViewCell {
  @IBOutlet weak var imageSliderHomeClient: UIImageView!
}

and i wasn't sure about how to show my connections in sotryboard but its the log right ?

2022-11-18 16:53:13.662034+0100 batigreenStoryboard[8572:197889] [Storyboard] Unknown class HomeClient in Interface Builder file.
2022-11-18 16:53:13.691509+0100 batigreenStoryboard[8572:197889] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIView 0x148d686c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key imageSliderHomeClient.'
*** First throw call stack:
(
	0  CoreFoundation           0x000000018040e7ec __exceptionPreprocess + 172
	1  libobjc.A.dylib           0x0000000180051144 objc_exception_throw + 56
	2  CoreFoundation           0x000000018040e4fc -[NSException init] + 0
	3  Foundation             0x0000000180b5205c -[NSObject(NSKeyValueCoding) setValue:forKey:] + 308
	4  UIKitCore              0x0000000102499284 -[UIView(CALayerDelegate) setValue:forKey:] + 156
	5  UIKitCore              0x0000000101c7c984 -[UIRuntimeOutletConnection connect] + 80
	6  CoreFoundation           0x00000001803f8994 -[NSArray makeObjectsPerformSelector:] + 192
	7  UIKitCore              0x0000000101c7478c -[UINib instantiateWithOwner:options:] + 1408
	8  UIKitCore              0x000000010197a028 -[UIViewController loadView] + 392
	9  UIKitCore              0x000000010197a2b0 -[UIViewController loadViewIfRequired] + 96
	10 UIKitCore              0x000000010197a800 -[UIViewController view] + 20
	11 UIKitCore              0x00000001018d2618 -[UINavigationController _startCustomTransition:] + 916
	12 UIKitCore              0x00000001018e3be8 -[UINavigationController _startDeferredTransitionIfNeeded:] + 492
	13 UIKitCore              0x00000001018e4ab8 -[UINavigationController __viewWillLayoutSubviews] + 92
	14 UIKitCore              0x00000001018cc2e0 -[UILayoutContainerView layoutSubviews] + 168
	15 UIKitCore              0x0000000102499bc8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1920
	16 QuartzCore             0x0000000187f18844 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 440
	17 QuartzCore             0x0000000187f232fc _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 128
	18 QuartzCore             0x0000000187e51058 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 452
	19 QuartzCore             0x0000000187e7ce28 _ZN2CA11Transaction6commitEv + 652
	20 QuartzCore             0x0000000187e7e25c _ZN2CA11Transaction25flush_as_runloop_observerEb + 68
	21 CoreFoundation           0x000000018037217c __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
	22 CoreFoundation           0x000000018036cb1c __CFRunLoopDoObservers + 512
	23 CoreFoundation           0x000000018036cfd4 __CFRunLoopRun + 968
	24 CoreFoundation           0x000000018036c7f4 CFRunLoopRunSpecific + 584
	25 GraphicsServices          0x0000000188faec98 GSEventRunModal + 160
	26 UIKitCore              0x00000001020005d4 -[UIApplication _run] + 868
	27 UIKitCore              0x00000001020045cc UIApplicationMain + 124
	28 libswiftUIKit.dylib         0x0000000101468fc0 $s5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF + 100
	29 batigreenStoryboard         0x0000000100781ebc $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 104
	30 batigreenStoryboard         0x0000000100781e44 $s19batigreenStoryboard11AppDelegateC5$mainyyFZ + 44
	31 batigreenStoryboard         0x0000000100781f40 main + 28
	32 dyld                0x0000000100ad9fa0 start_sim + 20
	33 ???                 0x00000001009ede50 0x0 + 4305378896
	34 ???                 0x8e0a000000000000 0x0 + 10234993103152873472
)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIView 0x148d686c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key imageSliderHomeClient.'
terminating with uncaught exception of type NSException
CoreSimulator 857.13 - Device: iPhone 14 Pro (6E6732CE-5770-4803-96B8-07D2C8BBAF0E) - Runtime: iOS 16.1 (20B72) - DeviceType: iPhone 14 Pro

Don't delete the content view of course.

Could you cmd-click on - Image Slider Home Client (which is in contentView) and post the popup window you get.

Note: are you french ? If you want, you could post a mail address for a short moment here so that we can share files. That will make it easier to analyse issue.

désolée pour la réponse lente j'étais très occupé la semaine dernière.

alors j'ai également un peu modifié mon code mais dans l'ensemble c'est exactement le même avec le même problème .

j'ai réduit le nombre d'erreur a un mais c'est toujours celui qui me pose probleme : imageCustomSliderHomeClient outlet from the HomeClient to the UIImageView is invalid. Outlets cannot be connected to repeating content.

cmd click sur mon image j'ai rien qui apparait par contre j'ai son outlet donc je vais le posté.

je rajoute mon mail dans le post quand j'ai une réponse

can't complete my collection View
 
 
Q