I'm I'm trying to show the collectionview in the tableview, but I keep getting the error index out of range, and the contents of the cells change when I scroll.What is this reason ? I tried many different ways but couldn't solve it
View Controller
var featuresVM = [FeaturesModel]()
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
configure()
}
func setupUI() {
tableView.register(CustomCell.self, forCellReuseIdentifier: CustomCell.identifier)
tableView.rowHeight = 200
featuresVM = [
FeaturesModel(imageName: "images", titleModel: [
Features(title: "Name", overview: "4"),
Features(title: "Price", overview: "5"),
Features(title: "Count", overview: "6"),
Features(title: "Date", overview: "7")]),
FeaturesModel(imageName: "images", titleModel: [
Features(title: "Ürün Adı", overview: "19"),
Features(title: "Ürün Fiyatı", overview: "20"),
Features(title: "Ürün Fiyatı", overview: "21"),
Features(title: "Ürün Fiyatı", overview: "22"),
Features(title: "Ürün Fiyatı", overview: "23"),
Features(title: "Ürün Fiyatı", overview: "24"),
Features(title: "S.K.T", overview: "25")]),
//And more
]
}
}
extension ViewController : UITableViewDataSource,UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return featuresVM.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return featuresVM[section].imageName
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CustomCell.identifier, for: indexPath) as! CustomCell
cell.viewModel = featuresVM[indexPath.section].titleModel
return cell
}
}
extension CustomCell : UICollectionViewDelegate , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return viewModel.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionViewCell.identifier, for: indexPath) as? CollectionViewCell else {
return UICollectionViewCell()
}
cell.titleView.text = viewModel[indexPath.row].title
cell.overView.text = viewModel[indexPath.row].overview
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: baseCollectionView.bounds.width / 2 , height: baseCollectionView.bounds.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}
I solve this problem , this problem reason collectionview datasource and delegate tableviewcell . I defined it in viewcontroller collectionview delegate and datasource and solved.