J. *_*Doe 6 ios uicollectionview uicollectionviewcell uicollectionviewlayout swift
cellForItemAt indexPath如果使用则不会打电话UICollectionViewDelegateFlowLayout。
在这种情况下:
class Something: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
}
Run Code Online (Sandbox Code Playgroud)
它叫我cellforItemAtIndexPath,但不叫我
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
}
Run Code Online (Sandbox Code Playgroud)
但如果我包括UICollectionViewDelegateFlowLayout:
class Something: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
}
Run Code Online (Sandbox Code Playgroud)
它会调用:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
}
Run Code Online (Sandbox Code Playgroud)
但不会打电话cellForItemAtIndexPath。我不明白这个问题。这是iOS的错误,还是什么都看不到?
小智 6
对我来说,在创建collectionViewwith 布局时,应该是let layout = UICollectionViewFlowLayout(),而不是let layout = UICollectionViewLayout()那么容易忽视。我被困在这种情况不止两次。
试试这个代码。
import UIKit
let kSCREENWIDTH = UIScreen.main.bounds.size.width
let kSCREENHEIGHT = UIScreen.main.bounds.size.height
let COLLECTION_CELL = "collectionCell"
class DemoController: UIViewController {
var collectionView : UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView()
}
/// create programatically collection view
fileprivate func setupCollectionView() {
let layout = UICollectionViewFlowLayout()
// layout.itemSize = CGSize(width:(kSCREENWIDTH-20)/2,height:150)
layout.sectionInset = UIEdgeInsetsMake(5, 7, 5, 7)
layout.minimumLineSpacing = 5
layout.minimumInteritemSpacing = 1
collectionView = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.backgroundColor = UIColor.white
self.view .addSubview(collectionView)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: COLLECTION_CELL)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//MARK: UICollectionViewDelegate
extension DemoController : UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// add your code here
}
}
//MARK: UICollectionViewDataSource
extension DemoController : UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
/// add your code here
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: COLLECTION_CELL, for: indexPath)
cell.backgroundColor = UIColor.lightGray
print("Here cellForItemAt Works")
return cell
}
}
//MARK: UICollectionViewDelegateFlowLayout
extension DemoController : UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
print("Here sizeForItemAt Works")
return CGSize(width: 150, height: 150)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3342 次 |
| 最近记录: |