页面控件不适用于UITableView中的UICollectionView



我在为表视图中的集合视图设置页面控件时遇到问题。视图控制器中实际上有两个集合视图,一个在tableView之外,另一个在内部。我想要一个内部的页面控制。让我分享下面的代码,然后解释是什么引起了麻烦:

class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
//setup stories collection view
let nib = UINib(nibName: "StoryCollectionViewCell", bundle: nil)
cvStories.register(nib, forCellWithReuseIdentifier: "StoryCollectionViewCell")
let layout1 = UICollectionViewFlowLayout()
layout1.scrollDirection = .horizontal
layout1.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
collectionViewStories.setCollectionViewLayout(layout1, animated: true)
collectionViewStories.delegate = self
collectionViewStories.dataSource = self

tblHome.dataSource = self
tblHome.delegate = self
}

// MARK: - Table View
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tblHome.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath) as! HomeTableViewCell

cell.collectionViewPostImages.dataSource = self
cell.collectionViewPostImages.delegate = self
cell.collectionViewPostImages.tag = indexPath.row
cell.collectionViewPostImages.reloadData()

cell.pageControl.hidesForSinglePage = true
cell.pageControl.numberOfPages = self.posts[cell.collectionViewPostImages.tag].imagesArray?.count ?? 0

return cell

}
//MARK: - Collection View
func numberOfSections(in collectionView: UICollectionView) -> Int {
if collectionView == cvStories {
return 2
}
else {
return 1
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == collectionViewStories {
return stories.count
}
else {
return posts[collectionView.tag].imagesArray?.count ?? 0
}
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

if collectionView == collectionViewStories {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "StoryCollectionViewCell", for: indexPath) as! StoryCollectionViewCell
//code to show stories
return cell

}
else {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionViewCell", for: indexPath) as! HomeCollectionViewCell
//code to show images
return cell

}

}
}
//TableViewCell Class
class HomeTableViewCell: UITableViewCell {

@IBOutlet weak var collectionViewPostImages: UICollectionView!
@IBOutlet weak var pageControl: UIPageControl!

var currentPage = 0

override func awakeFromNib() {

let flowLayout = UICollectionViewFlowLayout()
flowLayout.itemSize = CGSize(width: UIScreen.main.bounds.size.width, height: cvPostImages.frame.size.height)
flowLayout.scrollDirection = .horizontal
flowLayout.minimumInteritemSpacing = 0
flowLayout.minimumLineSpacing = 0
collectionViewPostImages?.collectionViewLayout = flowLayout
collectionViewPostImages.showsHorizontalScrollIndicator = false

collectionViewPostImages.isPagingEnabled = true

}

//ScrollView delegate method
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageWidth = scrollView.frame.width
self.currentPage = Int((scrollView.contentOffset.x + pageWidth / 2) / pageWidth)
self.pageControl.currentPage = self.currentPage
}

}

整个代码就像Instagram应用程序,故事在顶部,帖子中的图片在tableView中。整个代码都在一个View控制器中,一切都很好。偶数点(页码(根据页数是正确的,但当你在它们之间滑动时,它们不会改变。我看到了很多解决方案,在这些页面中,Control.numberOfPages被用于cellForItemAt函数,但它超出了我的范围,所以我只是在cellForRowAt中将其称为cell.pageControl.numberOfPages。第一次在tableView中处理collectionView,所以在工作时感到困惑。

主要问题是在HomeViewController中使用cell.collectionViewPostImages.delegate = self,但在单元格HomeTableViewCell中使用方法scrollViewDidScroll

Collectionview自己的滚动视图委托方法在UICollectionViewDelegate中触发。这就是为什么scrollViewDidScroll在你的细胞中没有被触发的原因。

解决方案是在删除集合视图的位置使用scrollViewDidScroll。它是HomeViewController

顺便说一句,我不建议在可重复使用的细胞中使用cell.collectionViewPostImages.dataSource = self cell.collectionViewPostImages.delegate = self。你不想每次细胞重复使用时都打开...delegate = self。你可以在细胞类的awakeFromNib中服用

最新更新