UICollectionViewCell is reordered or disappear on scroll - ios

I have a problem with UICollectionView. I created a collection with FlowLayout and I'm trying to resize only second item to make it width to 2xcellWidth. It's ok when View is appear, but after that, when I'm scrolling and back to top, my cell are disappear, reorder or change it width. It's totally weird behave.
So to be clear: I created collection view, scroll to bottom and when I back to top, I see my cells are in some weird place like this:
before:
after:
class MapEventsViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate {
var collectionView: UICollectionView!
let screenWidth = UIScreen.main.bounds.width
let options = [Options(id: "", value: "28-03-2017 10:54:01"),
Options(id: "", value: "Berlin Strasse 6A 00-000, Berlin", isDoubled: true),
Options(id: "Status", value: "-"),
Options(id: "Prędkość", value: "40km/h"),
Options(id: "Licznik", value: "250000km"),
Options(id: "Napięcie", value: "15V")]
override func viewDidLoad() {
super.viewDidLoad()
view.frame.size.height = 170
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 1, left: 0, bottom: 0, right: 1)
layout.minimumLineSpacing = 1
layout.minimumInteritemSpacing = 0
layout.scrollDirection = .vertical
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(MapEventCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.backgroundColor = UIColor.white
collectionView.isScrollEnabled = true
collectionView.alwaysBounceVertical = true
collectionView.autoresizingMask = UIViewAutoresizing.flexibleWidth
collectionView.backgroundColor = UIColor.init(white: 0.90, alpha: 1.0)
collectionView.showsVerticalScrollIndicator = true
self.view.addSubview(collectionView)
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return options.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MapEventCell
cell.awakeFromNib()
cell.id.font = UIFont.systemFont(ofSize: 12)
cell.value.font = UIFont.boldSystemFont(ofSize: 14)
cell.backgroundColor = UIColor.white
if indexPath.row == 0{
cell.value.font = UIFont.systemFont(ofSize: 10)
cell.value.frame.origin.y -= 6
}
if indexPath.row == 1{
cell.value.font = UIFont.systemFont(ofSize: 10)
cell.value.frame.origin.y -= 6
}
return cell
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
let thisCell = cell as! MapEventCell
thisCell.id.text = options[indexPath.item].id
thisCell.value.text = options[indexPath.item].value
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// print("sizeForItemAt")
let cellWidth = UIScreen.main.bounds.width / 3 - 1
if options[indexPath.item].isDoubled{
return CGSize(width: cellWidth * 2 + 1, height: 40)
}
return CGSize(width: cellWidth, height: 40)
}
}
class MapEventCell: UICollectionViewCell{
var id: UILabel = UILabel()
var value: UILabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func awakeFromNib() {
print("addViews")
id.frame = CGRect(x: 0, y: 0, width: contentView.frame.width, height: 16)
id.textAlignment = .center
value.frame = CGRect(x: 0, y: 16, width: contentView.frame.width, height: 20)
value.textAlignment = .center
// id.font = UIFont.systemFont(ofSize: 12)
id.textColor = UIColor.lightGray
// value.font = UIFont.boldSystemFont(ofSize: 14)
contentView.addSubview(id)
contentView.addSubview(value)
}
override func prepareForReuse() {
id.removeFromSuperview()
value.removeFromSuperview()
}
}
UPDATE
I've solved this problem by calculating frame of each element in cell at CellForItemAt indexPath. So now it looks like this (and worked):
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MapEventCell
cell.awakeFromNib()
cell.backgroundColor = UIColor.white
if indexPath.section == 0{
cell.value.frame = CGRect(x: 0, y: 0, width: (screenWidth / 3), height: 30)
if options[indexPath.item].isDoubled {
cell.value.frame = CGRect(x: 0, y: 0, width: (screenWidth / 3) * 2 - 2, height: 30)
cell.value.textAlignment = .center
}
cell.id.isHidden = true
cell.value.font = UIFont.systemFont(ofSize: 10)
}else{
//cell.frame = CGRect(x: 0, y: 0, width: (screenWidth / 3) - 1, height: 40)
cell.id.isHidden = false
cell.id.font = UIFont.systemFont(ofSize: 10)
cell.value.font = UIFont.boldSystemFont(ofSize: 12)
}
return cell
}
But now I'm not sure is it good solving. I know that every time cells are reusing, so my question is, are there better solutions for this problem?

Related

Two UICollectionViews in One UIViewController

I have two UICollectionViews in one view controller, however my first collection view is conforming to my second collection view's delegate and data source.
The problem is that the category collection view is conforming to the size of the service collection view and it's trying to return five cells.
How do I make my delegate and data source recognize and conform to the two different collection views? Thanks!
My App Screen Image
// HomeController
private let categoryCollectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 120, height: 120),
collectionViewLayout: UICollectionViewFlowLayout())
private let serviceCollectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 120, height: 120),
collectionViewLayout: UICollectionViewLayout())
fileprivate func configureUI() {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
popularCategoryCardView.addSubview(categoryCollectionView)
categoryCollectionView.backgroundColor = .white
categoryCollectionView.collectionViewLayout = layout
categoryCollectionView.isScrollEnabled = true
categoryCollectionView.delegate = self
categoryCollectionView.dataSource = self
categoryCollectionView.register(CategoryCell.self, forCellWithReuseIdentifier: CellIdentifiers.CategoryCell)
categoryCollectionView.anchor(left: popularCategoryCardView.leftAnchor, bottom: popularCategoryCardView.bottomAnchor,
right: popularCategoryCardView.rightAnchor, paddingLeft: 8, paddingBottom: 8, paddingRight: 8, height: 120)
popularServiceCardView.addSubview(serviceCollectionView)
serviceCollectionView.backgroundColor = .white
serviceCollectionView.collectionViewLayout = layout
serviceCollectionView.isScrollEnabled = true
serviceCollectionView.delegate = self
serviceCollectionView.dataSource = self
serviceCollectionView.register(ServiceCell.self, forCellWithReuseIdentifier: CellIdentifiers.ServiceCell)
serviceCollectionView.anchor(left: popularServiceCardView.leftAnchor, bottom: popularServiceCardView.bottomAnchor,
right: popularServiceCardView.rightAnchor, paddingLeft: 8, paddingBottom: 8, paddingRight: 8, height: 150)
}
// MARK: - UICollectionViewDataSource
extension HomeController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.categoryCollectionView {
return jobCategory.count
} else {
return 5
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.categoryCollectionView {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellIdentifiers.CategoryCell, for: indexPath) as! CategoryCell
cell.jobCategory = jobCategory[indexPath.item]
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellIdentifiers.ServiceCell, for: indexPath) as! ServiceCell
return cell
}
}
}
// MARK: - UICollectionViewDelegateFlowLayout
extension HomeController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView == self.categoryCollectionView {
return CGSize(width: 160, height: 120)
} else {
return CGSize(width: 100, height: 140)
}
}
}
Don't use the same layout object for multiple collection views:
let categoryLayout = UICollectionViewFlowLayout()
categoryLayout.scrollDirection = .horizontal
categoryCollectionView.collectionViewLayout = categoryLayout
let serviceLayout = UICollectionViewFlowLayout()
serviceLayout.scrollDirection = .horizontal
serviceCollectionView.collectionViewLayout = serviceLayout

MyUICollectionViewCell is not visible

import UIKit
private let reuseIdentifier = "NewCell"
class SeondViewController: UIViewController, UICollectionViewDelegateFlowLayout {
#IBOutlet var myNavBar: UINavigationBar!
var frutta: [String]!
override func viewDidLoad() {
super.viewDidLoad()
let margins = self.view.layoutMarginsGuide
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 30, left: 10, bottom: 10, right: 10)
layout.scrollDirection = .vertical
let newCollectionView: UICollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
newCollectionView.dataSource = self
newCollectionView.delegate = self
newCollectionView.register(SecondTabCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
newCollectionView.reloadData()
newCollectionView.backgroundColor = UIColor.clear
newCollectionView.isHidden = false
newCollectionView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(newCollectionView)
frutta = ["Mele", "Olive", "Pere", "Noci", "Banane", "Kiwi", "Ananas"]
myNavBar.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
myNavBar.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
myNavBar.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
newCollectionView.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
newCollectionView.topAnchor.constraint(equalTo: myNavBar.bottomAnchor).isActive = true
newCollectionView.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
◦ newCollectionView.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.size.width / 2, height: collectionView.bounds.size.height / 3)
}
}
extension SecondViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return frutta.count
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! SecondTabCollectionViewCell
myCell.backgroundColor = UIColor.clear
myCell.secondCellLabel = UILabel()
myCell.secondCellLabel.frame.size = CGSize(width: myCell.bounds.width / 2, height: myCell.secondCellLabel.bounds.height / 2)
myCell.secondCellLabel.adjustsFontSizeToFitWidth = true
myCell.secondCellLabel.backgroundColor = UIColor.clear
myCell.secondCellLabel.textColor = UIColor.black
myCell.secondCellLabel.text = frutta[indexPath.item]
myCell.translatesAutoresizingMaskIntoConstraints = false
myCell.contentView.addSubview(myCell.secondCellLabel)
return myCell
}
}
extension SecondViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Hai premuto \(indexPath.row).")
}
}
import UIKit
class SecondTabCollectionViewCell: UICollectionViewCell {
var secondCellLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) non e stato implementato.")
}
}
myCell.secondCellLabel.frame.size = CGSize(width: myCell.bounds.width / 2, height: myCell.secondCellLabel.bounds.height / 2)
The above line is wrong in cellForItemAt, you put the height of label is myCell.secondCellLabel.bounds.height instead of myCell.bounds.height.
correct one is
myCell.secondCellLabel.frame.size = CGSize(width: myCell.bounds.width / 2, height: myCell.bounds.height / 2)
It is a little strange to be laying out a cell's internal views in cellForRowAtIndexPath, this should be done in the cell class itself. The basic problem is that you your UILabel is appearing in the top left corner of the cell and has a height of zero, so you cannot see it. I moved the code you had in cellForRowAtIndexPath into the cell itself.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! SecondTabCollectionViewCell
myCell.backgroundColor = UIColor.clear
myCell.secondCellLabel.text = frutta[indexPath.item]
return myCell
}
import UIKit
class SecondTabCollectionViewCell: UICollectionViewCell {
var secondCellLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = .clear
label.textColor = .black
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .center
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
configureViews()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
private func configureViews() {
addSubview(secondCellLabel)
}
override func layoutSubviews() {
super.layoutSubviews()
configureLayout()
}
private func configureLayout() {
let width = bounds.width / 2
let height = frame.height / 2
let size = CGSize(width: width, height: height)
let origin = CGPoint(x: width/2, y: height/2)
secondCellLabel.frame = CGRect(origin: origin, size: size)
}
}

Swift how to resize UICollectionViewCell based on it's content

I have got this Swift code
let collectionView = UICollectionView(frame: CGRect(x:0,y:0,width:0,height:0),
collectionViewLayout: UICollectionViewFlowLayout())
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.register(PostCell.self, forCellWithReuseIdentifier: cellId)
collectionView.delegate = self
collectionView.dataSource = self
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PostCell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
return UIEdgeInsets(top: 15, left: 5, bottom: 15, right: 5)
}
My goal is to make cell's height automatic based on it's content.I wanted to implement
if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
}
and
let layoutHeight = UICollectionViewFlowLayout()
layoutHeight.estimatedItemSize = CGSize(width: 100, height: 100)
collectionView = UICollectionView(frame: CGRect(x:0,y:0,width:0,height:0), collectionViewLayout: layoutHeight)
But the first one made all cells 50x50 and the second one made them 100x100.Here is my PostCell class
class PostCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
designCell()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let CellprofileImage: UIImageView = {
let v = UIImageView()
v.layer.cornerRadius = 55
v.layer.masksToBounds = true
v.layer.borderWidth = 3
v.layer.borderColor = UIColor(white: 0.5, alpha: 1).cgColor
v.image = #imageLiteral(resourceName: "guitar")
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
func designCell(){
addSubview(CellprofileImage)
backgroundColor = .red
cellConstraints()
}
func cellConstraints(){
CellprofileImage.topAnchor.constraint(equalTo: self.topAnchor,constant:20).isActive = true
CellprofileImage.leftAnchor.constraint(equalTo: self.leftAnchor,constant:20).isActive = true
CellprofileImage.widthAnchor.constraint(equalToConstant: 310).isActive = true
CellprofileImage.heightAnchor.constraint(equalToConstant: 310).isActive = true
}
}

Items in UICollectionView disappeared on scrolling

I have an UICollectionViewController which will be displayed both on its own full screen mode as well as in a pop-up view of another controller. The full screen mode works perfectly. However, in the pop-up mode, all items disappears after the user scroll the content up and down for a few times (sometimes more than 10 times, but also happen at the first or second time)
let vc = storyboard.instantiateViewController(
withIdentifier: "myCollectionView") as? MyColle ctionViewController
// set up the overlap in the parent controller
let bounds = CGRect(x: x + 22, y: parentFrame.origin.y + dy,
width: width, height: parentFrame.height - dy - 10)
overlayView = UIView(frame: bounds)
overlayView!.layer.cornerRadius = 5.0
overlayView!.backgroundColor = UIColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 0.4)
self.view.addSubview(overlayView!)
vc.showAsPopup(overlayView!)
// in MyCollectionViewController, add itself to the parent popup view
func showAsPopup(_ parentView: UIView) {
view.backgroundColor = UIColor.red // test color to show the view is never gone
view.layer.cornerRadius = 5.0
collectionView?.backgroundColor = UIColor.clear
collectionView?.layer.cornerRadius = 5.0
parentView.addSubview(view)
view.frame = CGRect(x: 0, y: 20,
width: parentView.frame.width,
height: parentView.frame.height - 20)
NSLog("my view bounds after: \(view.frame.origin.x) \(view.frame.origin.y)")
NSLog(" : \(view.frame.width)x\(view.frame.height)")
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
NSLog("\(items.count) items in section \(section)")
return items.count
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let screenRect = view.bounds
let scale = UIScreen.main.scale
let width = screenRect.size.width - 20
return CGSize(width: width, height: 45 * scale)
}
override func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let item = items[indexPath.item]
let cellId = "RCCell_\(item.id)"
NSLog("creating cell \(cellId)")
// Register cell classes
self.collectionView!.register(RemoteControlViewCell.self,
forCellWithReuseIdentifier: cellId)
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId,
for: indexPath) as! RemoteControlViewCell
// Configure the cell, override the icon in Cell
cell.itemId = item.id
cell.itemIcon.image = item.icon
cell.itemDescriptionLabel.text = item.description
return cell
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 12.5, left: 0, bottom: 12.5, right: 0)
}
override func collectionView(_ collectionView: UICollectionView,
didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
NSLog("removed cell at \(indexPath.row)")
}
// MyCollectionViewCell
override init(frame: CGRect) {
super.init(frame: frame)
let scale = UIScreen.main.scale
NSLog("Cell frame w: \(frame.size.width) h: \(frame.size.height), scale \(scale)")
let spacing = CGFloat(10)
contentView.backgroundColor = UIColor.white
contentView.layer.cornerRadius = 5
contentView.layer.masksToBounds = true
itemIcon = UIImageView(frame: CGRect(x: spacing, y: spacing,
width: CGFloat(24.0), height: CGFloat(24.0)))
itemIcon.contentMode = UIViewContentMode.scaleAspectFit
contentView.addSubview(itemIcon)
NSLog("icon w: \(itemIcon.frame.size.width)")
let labelX = spacing + 5 + itemIcon.frame.size.width
let font = UIFont.systemFont(ofSize: 20)
itemDescriptionLabel = UILabel(
frame: CGRect(x: labelX, y: spacing,
width: frame.size.width - labelX - spacing,
height: font.lineHeight))
itemDescriptionLabel.textColor = UIColor(colorLiteralRed: 0x66 / 255.0, green: 0x66 / 255.0, blue: 0x66 / 255.0, alpha: 1.0)
moduleDescriptionLabel.font = font
contentView.addSubview(itemDescriptionLabel)
NSLog("item desc \(String(describing: itemDescriptionLabel.text)) w: \(itemDescriptionLabel.frame.size.width)")
}
I tried changing the background color of the collectionView to visible and can see that the collect view itself was never gone even though the items disappeared. I also added logging to the collectionView() callback methods and found that they were never called again after the initial view presentation.
What could cause the items to disappear during scrolling?
Try using prepareForReuse in RemoteControlViewCell class
override func prepareForReuse() {
itemId = ""
itemIcon.image = nil
itemDescriptionLabel.text = ""
}

UICollectionView overlapping cells

I am having a UICollectionView that I create and add programatically but for some reason at runtime section 1 and section 2 are overlapping, they both start in the same place:
Here is my Implementation (the code is swift 2.3 since this is a project requirement):
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = lightGreyColor
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 15, right: 0)
layout.minimumInteritemSpacing = 0.5
layout.minimumLineSpacing = 0.5
layout.scrollDirection = UICollectionViewScrollDirection.Vertical
// layout.headerReferenceSize = CGSize(width: view.bounds.width, height: 30)
let collectionViewFrame = CGRect(x: 0, y: 0, width: view.bounds.width, height: viewHeight)
collectionView = UICollectionView(frame: collectionViewFrame, collectionViewLayout: layout)
collectionView.allowsSelection = true
collectionView.bounces = true
collectionView.scrollEnabled = true
collectionView.hidden = false
collectionView.pagingEnabled = false
collectionView.backgroundColor = lightGreyColor
collectionView.showsVerticalScrollIndicator = false
collectionView.showsHorizontalScrollIndicator = false
collectionView.delegate = self
collectionView.dataSource = self
view.addSubview(collectionView)
collectionView.registerNib(UINib(nibName: "SmallDiscoverItemCollectionViewCell", bundle: NSBundle.mainBundle()), forCellWithReuseIdentifier: "SmallDiscoverItemCollectionViewCell")
collectionView.registerClass(NewDiscoverTopCollectionViewCell.self, forCellWithReuseIdentifier: "NewDiscoverTopCollectionViewCell")
collectionView.registerClass(TopHeaderCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "TopHeaderCell")
greedoCollectionViewLayout = nil
greedoCollectionViewLayout1().rowMaximumHeight = collectionView.bounds.width/1.8
greedoCollectionViewLayout1().fixedHeight = false
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
switch indexPath.section {
case 0:
return CGSize(width: collectionView.bounds.width, height: 200)
case 1:
return CGSize(width: collectionView.bounds.width, height: 200)
default:
return self.greedoCollectionViewLayout1().sizeForPhotoAtIndexPath(indexPath)
}
}
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
switch (indexPath.section) {
case 0: let cell = collectionView.dequeueReusableCellWithReuseIdentifier("NewDiscoverTopCollectionViewCell", forIndexPath: indexPath) as! NewDiscoverTopCollectionViewCell
return cell
case 1: let cell = collectionView.dequeueReusableCellWithReuseIdentifier("NewDiscoverTopCollectionViewCell", forIndexPath: indexPath) as! NewDiscoverTopCollectionViewCell
return cell
case 2: let cell = collectionView.dequeueReusableCellWithReuseIdentifier("SmallDiscoverItemCollectionViewCell", forIndexPath: indexPath) as! SmallDiscoverItemCollectionViewCell
let item = usersItems[indexPath.item]
return cell
}
This is the end result of the overlap: Section 0 and 1 and having green background while in between there is this gap. While section 2 has lots of images:
For some weird reason the UICollectionViewCell got initialise to a y position equal to its size. To fix that I have just added this in the UICollectionViewCell
override init(frame: CGRect) {
let noProblemFrame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
super.init(frame: noProblemFrame)

Resources