Ber*_*ngl 2 uicollectionview uicollectionviewcell swift
我已经设置了一个 UICollectionView ,如下所示。我正在尝试设置自定义单元格高度,以便集合在必须滚动之前适合屏幕上的 3 个单元格。但是,这种定制工作不起作用或似乎被忽略了。有谁知道我做错了什么?
class CoopOverviewViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var CampaignBrandSliderBackground: UIView!
@IBOutlet weak var CampaignBrandSlider: UISegmentedControl!
@IBOutlet weak var CampaignCollectionView: UICollectionView!
// TODO: load real data and replace with campaign model
let campaignImages: [UIImage] = [UIImage(named: "icon-black")!,UIImage(named: "icon-white")!,UIImage(named: "icon-black")!,UIImage(named: "icon-white")!]
let apiService = APIService()
override func viewDidLoad() {
super.viewDidLoad()
CampaignBrandSliderBackground.layer.cornerRadius = 10
// do other stuff...
// Conform to UICollectionViewDelegate Protocol:
CampaignCollectionView.dataSource = self
CampaignCollectionView.delegate = self
// Adjust Layout of CollectionViewCell: set cell height so that the collection fits 3 cells
var cellLayout = self.CampaignCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
cellLayout.itemSize = CGSize(width: self.CampaignCollectionView.frame.size.width, height: self.CampaignCollectionView.frame.size.height/3)
Run Code Online (Sandbox Code Playgroud)
// ???: 自定义高度似乎被忽略了 }
// Conform to UICollectionViewDataSource Protocol:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return campaignImages.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let campaignCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CampaignTileViewCell", for: indexPath) as! CampaignTileViewCellController
campaignCell.CampaignBackgroundImage.image = campaignImages[indexPath.item]
return campaignCell
}
Run Code Online (Sandbox Code Playgroud)
UICollectionView有一个Estimate Size属性,它限制了要设置的自定义高度。您可以从故事板更新其值。选择UICollectionView -> Size Inspector -> Estimate Size并将其值设置为None。这样你的自定义高度就会像魅力一样工作!
| 归档时间: |
|
| 查看次数: |
369 次 |
| 最近记录: |