ash*_*123 10 uitableview pagecontrol uicollectionview swift swift3
在我的项目中,我在collectionView中使用了tableView,并且还使用了pageControl.当我水平滑动集合视图时,我也想切换pageControl,但它无法正常工作.我使用scrollViewDidScroll方法切换pageControl为collectionViewSwipes.但问题是,当tableView scrollls scrollViewDidScroll方法将再次调用时.那么有什么不同的方法或任何解决方案来解决这个问题.
请查看以下链接.
TableView方法
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return model.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FbHomeCell
cell.btnPushImage.tag = indexPath.row
cell.collectionView.tag = indexPath.row
return cell
}
Run Code Online (Sandbox Code Playgroud)
CollectionView方法
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return model[collectionView.tag].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
collCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! FBHomeCollectionCell
collCell.backgroundColor = model[collectionView.tag][indexPath.item]
collCell.pageControlSwipe.numberOfPages = Int(collectionView.contentSize.width/collectionView.frame.size.width)
return collCell
}
Run Code Online (Sandbox Code Playgroud)
滚动视图委派方法
func scrollViewDidScroll(_ scrollView: UIScrollView)
{
let pageWidth = scrollView.frame.width
collCell.pageControlSwipe.currentPage = Int((scrollView.contentOffset.x + pageWidth / 2) / pageWidth)
collCell = cell.collectionView.cellForItem(at: IndexPath (item: scrollView.tag, section: 0)) as! FBHomeCollectionCell
}
Run Code Online (Sandbox Code Playgroud)
谢谢,感谢您的帮助..
Nir*_*v D 13
您可以比较现在正在滚动的视图scrollViewDidScroll.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == self.collectionView { //Your scrollView outlet
//Your code here
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:首先,您需要使用tableViewCell实现collectionView的数据源和委托方法,而不是使用ViewController,您还需要将pageControlSwipetableViewCell 放入collectionViewCell.所以看起来应该是这样的.
class FbHomeCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate {
@IBOutlet var collectionView: UICollectionView!
@IBOutlet var pageControlSwipe: UIPageControl!
@IBOutlet var btnPushImage: UIButton!
var colorArray = [UIColor]()
var currentPage = 0
func setCollectionViewWith(colorArray: [UIColor]) {
self.colorArray = colorArray
self.collectionView.isPagingEnabled = true
self.collectionView.datasource = self
self.collectionView.delegate = self
self.collectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return colorArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let collCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! FBHomeCollectionCell
collCell.backgroundColor = self.colorArray[indexPath.item]
self.pageControlSwipe.numberOfPages = colorArray.count
return collCell
}
//ScrollView delegate method
func scrollViewDidScroll(_ scrollView: UIScrollView)
{
let pageWidth = scrollView.frame.width
self.currentPage = Int((scrollView.contentOffset.x + pageWidth / 2) / pageWidth)
self.pageControlSwipe.currentPage = self.currentPage
}
}
Run Code Online (Sandbox Code Playgroud)
现在在tableView的数据源方法中调用此方法,如下所示.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return model.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FbHomeCell
cell.btnPushImage.tag = indexPath.row
cell.collectionView.tag = indexPath.row
cell.setCollectionViewWith(colorArray: model[indexPath.row])
return cell
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5625 次 |
| 最近记录: |