我在另一个uicollectionview中有一个uicollectionview。当我水平滚动单元格时,如您所见,“顶部单元格”(包含其他集合视图的单元格)未出列:
如您所见,“Randonnées”与“Aborigènes”相同。
我已尽力尝试prepareForReuse(),但它似乎无法正常工作。我知道其他人遇到了一个非常相似的问题,但没有一个相关的主题对我有用:/
以下是我的一些代码:
class X3ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
// MARK: Variables
let cellID = "pouet"
let delegate = UIApplication.shared.delegate as! AppDelegate
var cell : X3ContentCollectionViewCell?
// MARK: UI
let titleView : UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
var mainCollectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
view.collectionViewLayout = layout
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
view.isPagingEnabled = true
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .white
return view
}()
var menuBar: X3MenuBar?
override func viewWillAppear(_ animated: Bool) {
print(delegate.ds.X3data)
}
// MARK: ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.blue
mainCollectionView.dataSource = self
mainCollectionView.delegate = self
mainCollectionView.register(X3ContentCollectionViewCell.self, forCellWithReuseIdentifier: cellID)
menuBar = X3MenuBar()
menuBar?.view.translatesAutoresizingMaskIntoConstraints = false
menuBar?.rootController = self
addMenuScrollView()
addMainView()
}
// MARK: Methods
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return delegate.ds.X3data!.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
cell = mainCollectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as? X3ContentCollectionViewCell
cell?.indexOfContent = indexPath.row
return cell!
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height-25)
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let screenWidth = UIScreen.main.bounds.size.width
let itemIndex = Int(targetContentOffset.pointee.x / screenWidth)
menuBar?.scrollToItemIndex(item: itemIndex)
}
func scrollToItemIndex(item: Int) {
mainCollectionView.selectItem(at: IndexPath(row: item, section: 0), animated: true, scrollPosition: .centeredHorizontally)
delegate.ds.X3index = item
}
//MARK: UI Methods
func addMainView() {
view.addSubview(mainCollectionView)
mainCollectionView.topAnchor.constraint(equalTo: (menuBar?.view.bottomAnchor)!).isActive = true
mainCollectionView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
mainCollectionView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
mainCollectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
func addTitleView() {
view.addSubview(titleView)
titleView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
titleView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
titleView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
titleView.heightAnchor.constraint(equalToConstant: delegate.gfx.X1ViewTitleHeigth!).isActive = true
}
func addMenuScrollView() {
view.addSubview((menuBar?.view)!)
menuBar?.view.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
menuBar?.view.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
menuBar?.view.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
menuBar?.view.heightAnchor.constraint(equalToConstant: 30).isActive = true
}
deinit {
delegate.ds.X3data = nil
}
}
另一个重要部分:
class X3ContentCollectionViewCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
let cellId = "pouet"
let delegate = UIApplication.shared.delegate as! AppDelegate
var indexOfContent = 0
var customCollectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.collectionViewLayout = layout
cv.backgroundColor = .white
cv.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
cv.translatesAutoresizingMaskIntoConstraints = false
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
customCollectionView.register(X3ContentMenuCellCollectionViewCell.self, forCellWithReuseIdentifier: cellId)
customCollectionView.dataSource = self
customCollectionView.delegate = self
addSubview(customCollectionView)
customCollectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true
customCollectionView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
customCollectionView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
customCollectionView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -5).isActive = true
customCollectionView.reloadData()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return ((delegate.ds.X3data?[self.indexOfContent] as! NSDictionary)["images"] as! NSArray).count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.item == 0 {
return CGSize(width: frame.size.width, height: 170)
} else if indexPath.item == 3 {
return CGSize(width: frame.size.width, height: 170)
}else {
return CGSize(width: frame.size.width/2, height: 120)
}
}
override func prepareForReuse() {
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = customCollectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! X3ContentMenuCellCollectionViewCell
print((((delegate.ds.X3data?[self.indexOfContent] as! NSDictionary)["images"] as! NSArray)[indexPath.row] as! NSDictionary)["image"])
cell.X3Label.text = "\((((delegate.ds.X3data![self.indexOfContent] as! NSDictionary)["images"] as! NSArray)[indexPath.row] as! NSDictionary)["image"]!) \(self.indexOfContent)"
cell.X3ImageView.image = UIImage(named: (((delegate.ds.X3data?[self.indexOfContent] as! NSDictionary)["images"] as! NSArray)[indexPath.row] as! NSDictionary)["image"] as! String)
cell.X3ImageView.contentMode = .scaleAspectFill
cell.X3ImageView.clipsToBounds = true
if indexPath.item == 0 {
cell.X3Label.font = delegate.gfx.font(style: 2)
} else if indexPath.item == 3 {
cell.X3Label.font = delegate.gfx.font(style: 2)
}else {
cell.X3Label.font = delegate.gfx.font(style: 4)
}
return cell
}
deinit {
}
}
答案 0 :(得分:1)
var indexOfContent = 0 {
didSet {
customCollectionView.reloadData()
}
}
答案 1 :(得分:0)
如果我正确理解你。当您需要重新加载内容时,您无法调用reloadData()。
另外,如果你要覆盖prepareForReuse(),为什么还有一个空的实现