UICollectionView Inside of UItableViewCell no horizontal scrolling

Hi, i'm having this issue using Xcode 12.2 were i can't scroll in horizontal direction a UICollectionView, i have try multiples answers from the forum and none of then work for me

first of all what i'm only trying to achieve is the scroll horizontally.

Heres my UITableView declaration on the View:
Code Block
let tableSeasons: UITableView = {
let table = UITableView(frame: CGRect.zero)
table.register(NumberOfSeasonsCell.self, forCellReuseIdentifier: "seasonsRegister")
table.backgroundColor = .clear
table.layer.cornerRadius = 0
table.separatorColor = .clear
table.translatesAutoresizingMaskIntoConstraints = false
table.showsHorizontalScrollIndicator = false
table.showsVerticalScrollIndicator = false
return table
}()

here's the Delegate and DataSource protocols in VC:
Code Block
extension SelectedShowVC: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayOfSeasonsInt.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "seasonsRegister", for: indexPath) as? NumberOfSeasonsCell else {
return UITableViewCell()
}
cell.backgroundColor = .clear
cell.selectionStyle = .none
cell.seasonsCollectionView.delegate = self
cell.seasonsCollectionView.dataSource = self
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}


This is the code for UICollectionView declaration on the NumberOfSeasonsCell or UITableViewCell
Code Block
lazy var seasonsCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 15
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.clear
cv.register(SeasonCell.self, forCellWithReuseIdentifier: "identifierSeasons")
cv.showsHorizontalScrollIndicator = false
cv.translatesAutoresizingMaskIntoConstraints = false
return cv
}()

Protocols for the UICollectionView
Code Block
extension NumberOfSeasonsCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arraySeasonsEpisodes.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "identifierSeasons", for: indexPath) as! SeasonCell
cell.backgroundColor = .clear
cell.model = arraySeasonsEpisodes[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 150, height: 100)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 15
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath.row)
}
}

i've read that i should store the last position of the Table for scrolling the UICollectionView but i've tried but not work for me

here's the UICollectionViewCell
Code Block
class SeasonCell: UICollectionViewCell {
var model: episodess? {
didSet {
guard let viewModel = model else { return }
let guardado = viewModel.still_path
let number = String(viewModel.episode_number ?? 0)
episodeNumber.text = "Episode Number: \(number)"
episodeName.text = "Episode Name: \(viewModel.name ?? "")"
let image = "500\(guardado ?? "")"
if image == "" || image == "undefined" {
seasonImage.image = UIImage(named: "")
seasonImage.contentMode = .scaleAspectFill
}else{
seasonImage.downloaded(from: image)
seasonImage.contentMode = .scaleAspectFill
}
}
}
override func prepareForReuse() {
super.prepareForReuse()
}
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
let seasonImage: UIImageView = {
let iv = UIImageView()
iv.contentMode = .scaleToFill
iv.backgroundColor = .clear
iv.image = UIImage(named: "")
iv.translatesAutoresizingMaskIntoConstraints = false
return iv
}()
var episodeNumber: UILabel = {
let label = UILabel()
label.text = ""
label.numberOfLines = 1
label.textAlignment = .left
label.font = Fonts.AVENIR.of(size: 10)
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor.white
return label
}()
var episodeName: UILabel = {
let label = UILabel()
label.text = ""
label.numberOfLines = 1
label.textAlignment = .left
label.font = Fonts.AVENIR.of(size: 10)
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor.white
return label
}()
func setupViews() {
addSubview(seasonImage)
addSubview(episodeNumber)
addSubview(episodeName)
NSLayoutConstraint.activate([
seasonImage.centerYAnchor.constraint(equalTo: self.centerYAnchor),
seasonImage.centerXAnchor.constraint(equalTo: self.centerXAnchor),
seasonImage.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1),
seasonImage.heightAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.6),
episodeNumber.leftAnchor.constraint(equalTo: seasonImage.leftAnchor),
episodeNumber.topAnchor.constraint(equalTo: seasonImage.bottomAnchor, constant: 8),
episodeNumber.rightAnchor.constraint(equalTo: seasonImage.rightAnchor),
episodeName.leftAnchor.constraint(equalTo: episodeNumber.leftAnchor),
episodeName.rightAnchor.constraint(equalTo: seasonImage.rightAnchor),
episodeName.topAnchor.constraint(equalTo: episodeNumber.bottomAnchor, constant: 8),
])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

Accepted Reply

The problem was an extra UITableViewCellContentView overlay appears in a TableView preventing taps so the UICollectionView couldn't get scrolled horizontally

adding this extension works

Code Block
extension UITableViewCell {
    open override func addSubview(_ view: UIView) {
        super.addSubview(view)
        sendSubviewToBack(contentView)
    }
}


Replies

i can't scroll in horizontal direction a UICollectionView

What is the exact behaviour ?
  • do you see the full content wrapping around on next lines ? Or part of the collection remains hidden on the right ?

  • What happens when you click and drag in order to scroll ? Any change on the table cell ?

Did you read this ?
https ://ashfurrow. com/ blog/ putting-a-uicollectionview-in-a-uitableviewcell-in-swift/
Hi, the exact behaviour is


do you see the full content wrapping around on next lines ? Or part of the collection remains hidden on the right ?

the information charge fine in each UItableViewCell and part of the collection remains hidden on the right because is not scrollable.


What happens when you click and drag in order to scroll ? Any change on the table cell ?

while scrolling the UITableViewCells works fine you can scroll down and up, and cells information charge , but the UICollectionViews inside each UITableViewCells are not scrollable horizontally, and the information charge fine


and about the link that was literally the first solution i tried
The problem was an extra UITableViewCellContentView overlay appears in a TableView preventing taps so the UICollectionView couldn't get scrolled horizontally

adding this extension works

Code Block
extension UITableViewCell {
    open override func addSubview(_ view: UIView) {
        super.addSubview(view)
        sendSubviewToBack(contentView)
    }
}


The accepted answer is not recommended. Remember, with the extension you are overriding the default behavior of the UITableViewCell. And the change you made will affect your whole project. You do not have to send the contentView to Back. Your mistake is in adding subviews directly to the cell. Instead, you should do:

self.contentView.addSubview(someView)

I had the same issue, and resolved by adding subviews to the cells contentView.