func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {}
这是我的密码:
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .white
cv.dataSource = self
cv.delegate = self
cv.isPagingEnabled = true
return cv
}()
var pages: [Page] = []
2-自定义集合ViewCell:PageCell
import UIKit
import Highcharts
import SwiftDataTables
import ChameleonFramework
class PageCell: UICollectionViewCell {
var imageView: UIImageView = {
let iv = UIImageView()
iv.contentMode = .scaleAspectFill
iv.backgroundColor = .yellow
iv.image = UIImage(named: "background_2")
iv.clipsToBounds = true
return iv
}()
var pageView: UIView= {
let pv = UIView()
pv.contentMode = .scaleAspectFill
pv.clipsToBounds = true
return pv
}()
var page: Page? {
didSet {
// print(page?.mainView)
guard let unwrappedPage = page else {
return
}
//imageView.image = UIImage(named: unwrappedPage.image)//unwrappedPage.image
pageView = unwrappedPage.mainView
imageView.image = UIImage(named: unwrappedPage.image)
print("imageView.image: ", imageView.image)
print("PageViewContent: ", pageView)
print("unwrappedPage.mainView: ", unwrappedPage.mainView)
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
func setupViews() {
addSubview(imageView)
imageView.anchorWithConstantsToTop(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, topConstant: 70, leftConstant: 0, bottomConstant: 100, rightConstant: 0)
addSubview(pageView)
pageView.anchorWithConstantsToTop(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, topConstant: 70, leftConstant: 0, bottomConstant: 100, rightConstant: 0)
//imageView.frame = CGRect(x: UIScreen.main.bounds.origin.x, y: UIScreen.main.bounds.origin.y+70, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height-100)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
3-自定义单元格的结构:第页
import Foundation
struct Page {
let mainView: UIView
let image: String
}
2-将collectionView添加到视图+设置约束
//TODO: collectionView Config
self.view.addSubview(self.collectionView)
self.collectionView.anchorToTop(top: self.view.topAnchor, left:
self.view.leftAnchor, bottom: self.view.bottomAnchor, right:
self.view.rightAnchor)
self.collectionView.register(PageCell.self, forCellWithReuseIdentifier: "cellId")
self.pages = {
let firstPage = Page(mainView: self.chartView, image: "background_2")
let secondPage = Page(mainView: self.chartView, image: "background_2")
return [firstPage, secondPage]
}()
3-CollectionView方法:
//MARK: - UICollectionViewController Extension
extension SecondViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
//TODO: numberOfItemsInSection method
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pages.count
}
//TODO: cellForItemAt method
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! PageCell
let page = pages[indexPath.item]
cell.page = page
return cell
}
//TODO: collectionViewLayout method
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}
//TODO: scrollViewWillEndDragging method
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let x = targetContentOffset.pointee.x
pageControl.currentPage = Int(x / view.frame.width)
}
}
self.collectionView.addSubview(self.chartView)
self.chartView.anchorWithConstantsToTop(top: self.view.topAnchor, left: self.view.leftAnchor, bottom: self.view.bottomAnchor, right: self.view.rightAnchor, topConstant: 50, leftConstant: 0, bottomConstant: 20, rightConstant: 0)
self.collectionView.addSubview(self.dataTable)
self.dataTable.anchorWithConstantsToTop(top: self.view.topAnchor, left: self.view.leftAnchor, bottom: self.view.bottomAnchor, right: self.view.rightAnchor, topConstant: 70, leftConstant: 0, bottomConstant: 20, rightConstant: 0)
这是唯一一种可以工作但不能满足我需要的情况,因为它们显示在每个单元格的另一个单元格的顶部:/。
到目前为止,我已经尝试过但不适用于我的:
我发现奇怪的是,它与UIImageView完美配合,但与图表或数据表却无法配合,除非我在collectionView配置之后添加它们。
提前感谢(y)