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!
}