如何解决Swift:无法滚动浏览在TableViewCell中以编程方式实现的collectionView
免责声明:之前也曾提出过类似的问题,但是collectionView的实现并不相同,因此提供的答案无济于事。
这是管理collectionView的代码:
extension TableViewCell2: UICollectionViewDelegate,UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
if (collectionView == collectionViewTest){
print("INSIDE TableViewCell2.collectionView 1. collectionViewTest detected")
print("INSIDE TableViewCell2.collectionView 1. moviesItems.count: ",moviesItems.count)
// This gets printed 20 times
return moviesItems.count
}
else {
print("first function other collection was detecteddetected")
print("INSIDE TableViewCell2.collectionView 1. moviesItems2.count: ",moviesItems2.count) //0
return moviesItems2.count
}
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print("INSIDE TableViewCell2.collectionView 2")
// This gets printed 7 times like the number of cells.
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:
cellIdentifier,for: indexPath) as! movieCardCell
cell.movieImageView.sd_setImage(with: URL(string: moviesItems[indexPath.item].imageURL))
return cell
}
}
注意::返回的单元格数为20,但仅显示7。因为仅显示7个collectionView单元格,所以哪一种有意义。
这是在tableViewCell内部实现collectionView的方式:
func setupCollectionView2() {
let layout = UICollectionViewFlowLayout()
collectionViewTest = UICollectionView(frame: CGRect.zero,collectionViewLayout: layout)
collectionViewTest.backgroundColor = UIColor (hex: "444A64")
collectionViewTest.register(UICollectionViewCell.self,forCellWithReuseIdentifier: "MyCell")
collectionViewTest.showsVerticalScrollIndicator = false
collectionViewTest.showsHorizontalScrollIndicator = false
self.contentView.addSubview(collectionViewTest)
collectionViewTest.delegate = self
collectionViewTest.dataSource = self
let nib = UINib(nibName: "movieCardCell",bundle: nil)
collectionViewTest.register(nib,forCellWithReuseIdentifier: cellIdentifier)
collectionViewTest.translatesAutoresizingMaskIntoConstraints = false
collectionViewTest.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor,constant: 0).isActive = true
collectionViewTest.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor,constant: 0).isActive = true
collectionViewTest.topAnchor.constraint(equalTo: self.contentView.topAnchor,constant: 0).isActive = true
collectionViewTest.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor,constant: 0).isActive = true
layout.scrollDirection = .horizontal
}
问题: 为什么我不能在collectionView中滚动以及如何解决这个问题?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。