ViewController类:UIViewController,UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,UICollectionViewDelegate {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "TableViewCell")
self.tableView.delegate = self
self.tableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView:UITableView,numberOfRowsInSection部分:Int)-> Int { 返回20 }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
cell.collectionView.tag = indexPath.row
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 450
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let cell1 = cell as? TableViewCell else {return}
cell1.collectionView.reloadData()
let iP = cell1.collectionView.indexPathsForVisibleItems.first
cell1.pageControl.currentPage = iP?.row ?? 0
print(iP ?? 0)
}
}
TableViewCell类:UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate {
// var thisWidth:CGFloat = 0
var currentPages = 0
var arr = ["5Health","4Science","1business","0startup"]
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var imgViewRound: UIImageView!
@IBOutlet weak var imgView: UIImageView!
@IBOutlet weak var collectionView: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
self.collectionView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionViewCell")
self.collectionView.delegate = self
self.collectionView.dataSource = self
self.imgViewRound.layer.cornerRadius = 16
pageControl.hidesForSinglePage = true
pageControl.numberOfPages = arr.count
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView:UICollectionView,numberOfItemsInSection部分:Int)-> Int {
return arr.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView
.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
cell.imgViewScroll.image = UIImage(named: arr[indexPath.row])
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width, height: collectionView.frame.size.height)
}
func scrollViewDidScroll(_ scrollView:UIScrollView) {
if scrollView == self.collectionView {
let pageWidth = scrollView.frame.width
self.currentPages = Int((scrollView.contentOffset.x + pageWidth / 2) / pageWidth)
self.pageControl.currentPage = currentPages
}
} func collectionView(_ collectionView:UICollectionView,willDisplay单元格:UICollectionViewCell,forItemAt indexPath:IndexPath){
pageControl.currentPage = Int(collectionView.contentOffset.x) / Int(collectionView.frame.width)
}
}