Single CollectionView with multiple CollectionViewLayout.why Layout is mess up?












1














I have Single UICollectionView , and I want to Apply Two different layout dynamically.





  1. UICollectionViewFlowLayout : A Layout with same size cell and circle image.



    var flowLayout: UICollectionViewFlowLayout {
    let flowLayout = UICollectionViewFlowLayout()

    flowLayout.itemSize = CGSize(width: UIScreen.main.bounds.width/3, height: 140)
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
    flowLayout.scrollDirection = UICollectionViewScrollDirection.vertical
    flowLayout.minimumInteritemSpacing = 0.0

    return flowLayout
    }


  2. Pintrest Layout :
    https://www.raywenderlich.com/392-uicollectionview-custom-layout-tutorial-pinterest



For Example : when user Click on Profile Button FlowLayout will be Apllied and Cell Appear with image in Circle Shape. when user click on Picture button pintrest layout will be Applied and cell Appear with image in Rectangle shape with dynamic height.



enter image description here



intially CollectionView have 1.flowLayout and it appears perfectly.but when I click on Picture button Pintrest layout is messed up with previous layout as shown in above image.



Following is Code For changing Layout.



if isGrid {
let horizontal = flowLayout
recentCollectionView.setCollectionViewLayout(horizontal, animated: true)
recentCollectionView.reloadData()
}
else {
let horizontal = PinterestLayout()
horizontal.delegate = self
recentCollectionView.setCollectionViewLayout(horizontal, animated: true)
recentCollectionView.reloadData()
}


ViewHiarchy:



I have main Collection-view that Contain header and one bottom cell.cell contain other Collection-view to which I am Applying multiple layout.I have Two Different cell for each layout.I want bottom cell size equal to content Collection-view Content size so user can Scroll entire main collection-view vertically.



 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell : UICollectionViewCell!

switch isGrid {
case true:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SearchProfileCell", for: indexPath)
if let annotateCell = cell as? SearchProfileCell {
annotateCell.photo = photos[indexPath.item]
}
case false:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AnnotatedPhotoCell", for: indexPath)

if let annotateCell = cell as? AnnotatedPhotoCell {
annotateCell.cellwidth = collectionView.contentSize.width/3
annotateCell.photo = photos[indexPath.item]

}
}

cell.contentView.layer.cornerRadius = 0
return cell
}


Code of profile and picture button Action.



@IBAction func pictureClick(sender:UIButton) {
isGrid = false
self.searchCollectionView.reloadData()
}

@IBAction func profilClick(sender:UIButton) {
isGrid = true
self.searchCollectionView.reloadData()
}









share|improve this question
























  • try performBatchUpdates
    – Prashant Tukadiya
    Nov 13 at 5:39










  • do you mean instead of reloadData i should use PerformBatchUpdate right?
    – Bhavesh.iosDev
    Nov 13 at 6:24










  • Inside block of performBatchUpdate put your layout reset code
    – Prashant Tukadiya
    Nov 13 at 6:30










  • okay.let me try
    – Bhavesh.iosDev
    Nov 13 at 6:34










  • I would think you should use layoutSubviews instead of reloadData() so after your IF Statement try self.view.layoutSubviews. You want to trigger a redraw of the view controller
    – Alexander
    Nov 13 at 7:49


















1














I have Single UICollectionView , and I want to Apply Two different layout dynamically.





  1. UICollectionViewFlowLayout : A Layout with same size cell and circle image.



    var flowLayout: UICollectionViewFlowLayout {
    let flowLayout = UICollectionViewFlowLayout()

    flowLayout.itemSize = CGSize(width: UIScreen.main.bounds.width/3, height: 140)
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
    flowLayout.scrollDirection = UICollectionViewScrollDirection.vertical
    flowLayout.minimumInteritemSpacing = 0.0

    return flowLayout
    }


  2. Pintrest Layout :
    https://www.raywenderlich.com/392-uicollectionview-custom-layout-tutorial-pinterest



For Example : when user Click on Profile Button FlowLayout will be Apllied and Cell Appear with image in Circle Shape. when user click on Picture button pintrest layout will be Applied and cell Appear with image in Rectangle shape with dynamic height.



enter image description here



intially CollectionView have 1.flowLayout and it appears perfectly.but when I click on Picture button Pintrest layout is messed up with previous layout as shown in above image.



Following is Code For changing Layout.



if isGrid {
let horizontal = flowLayout
recentCollectionView.setCollectionViewLayout(horizontal, animated: true)
recentCollectionView.reloadData()
}
else {
let horizontal = PinterestLayout()
horizontal.delegate = self
recentCollectionView.setCollectionViewLayout(horizontal, animated: true)
recentCollectionView.reloadData()
}


ViewHiarchy:



I have main Collection-view that Contain header and one bottom cell.cell contain other Collection-view to which I am Applying multiple layout.I have Two Different cell for each layout.I want bottom cell size equal to content Collection-view Content size so user can Scroll entire main collection-view vertically.



 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell : UICollectionViewCell!

switch isGrid {
case true:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SearchProfileCell", for: indexPath)
if let annotateCell = cell as? SearchProfileCell {
annotateCell.photo = photos[indexPath.item]
}
case false:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AnnotatedPhotoCell", for: indexPath)

if let annotateCell = cell as? AnnotatedPhotoCell {
annotateCell.cellwidth = collectionView.contentSize.width/3
annotateCell.photo = photos[indexPath.item]

}
}

cell.contentView.layer.cornerRadius = 0
return cell
}


Code of profile and picture button Action.



@IBAction func pictureClick(sender:UIButton) {
isGrid = false
self.searchCollectionView.reloadData()
}

@IBAction func profilClick(sender:UIButton) {
isGrid = true
self.searchCollectionView.reloadData()
}









share|improve this question
























  • try performBatchUpdates
    – Prashant Tukadiya
    Nov 13 at 5:39










  • do you mean instead of reloadData i should use PerformBatchUpdate right?
    – Bhavesh.iosDev
    Nov 13 at 6:24










  • Inside block of performBatchUpdate put your layout reset code
    – Prashant Tukadiya
    Nov 13 at 6:30










  • okay.let me try
    – Bhavesh.iosDev
    Nov 13 at 6:34










  • I would think you should use layoutSubviews instead of reloadData() so after your IF Statement try self.view.layoutSubviews. You want to trigger a redraw of the view controller
    – Alexander
    Nov 13 at 7:49
















1












1








1







I have Single UICollectionView , and I want to Apply Two different layout dynamically.





  1. UICollectionViewFlowLayout : A Layout with same size cell and circle image.



    var flowLayout: UICollectionViewFlowLayout {
    let flowLayout = UICollectionViewFlowLayout()

    flowLayout.itemSize = CGSize(width: UIScreen.main.bounds.width/3, height: 140)
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
    flowLayout.scrollDirection = UICollectionViewScrollDirection.vertical
    flowLayout.minimumInteritemSpacing = 0.0

    return flowLayout
    }


  2. Pintrest Layout :
    https://www.raywenderlich.com/392-uicollectionview-custom-layout-tutorial-pinterest



For Example : when user Click on Profile Button FlowLayout will be Apllied and Cell Appear with image in Circle Shape. when user click on Picture button pintrest layout will be Applied and cell Appear with image in Rectangle shape with dynamic height.



enter image description here



intially CollectionView have 1.flowLayout and it appears perfectly.but when I click on Picture button Pintrest layout is messed up with previous layout as shown in above image.



Following is Code For changing Layout.



if isGrid {
let horizontal = flowLayout
recentCollectionView.setCollectionViewLayout(horizontal, animated: true)
recentCollectionView.reloadData()
}
else {
let horizontal = PinterestLayout()
horizontal.delegate = self
recentCollectionView.setCollectionViewLayout(horizontal, animated: true)
recentCollectionView.reloadData()
}


ViewHiarchy:



I have main Collection-view that Contain header and one bottom cell.cell contain other Collection-view to which I am Applying multiple layout.I have Two Different cell for each layout.I want bottom cell size equal to content Collection-view Content size so user can Scroll entire main collection-view vertically.



 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell : UICollectionViewCell!

switch isGrid {
case true:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SearchProfileCell", for: indexPath)
if let annotateCell = cell as? SearchProfileCell {
annotateCell.photo = photos[indexPath.item]
}
case false:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AnnotatedPhotoCell", for: indexPath)

if let annotateCell = cell as? AnnotatedPhotoCell {
annotateCell.cellwidth = collectionView.contentSize.width/3
annotateCell.photo = photos[indexPath.item]

}
}

cell.contentView.layer.cornerRadius = 0
return cell
}


Code of profile and picture button Action.



@IBAction func pictureClick(sender:UIButton) {
isGrid = false
self.searchCollectionView.reloadData()
}

@IBAction func profilClick(sender:UIButton) {
isGrid = true
self.searchCollectionView.reloadData()
}









share|improve this question















I have Single UICollectionView , and I want to Apply Two different layout dynamically.





  1. UICollectionViewFlowLayout : A Layout with same size cell and circle image.



    var flowLayout: UICollectionViewFlowLayout {
    let flowLayout = UICollectionViewFlowLayout()

    flowLayout.itemSize = CGSize(width: UIScreen.main.bounds.width/3, height: 140)
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
    flowLayout.scrollDirection = UICollectionViewScrollDirection.vertical
    flowLayout.minimumInteritemSpacing = 0.0

    return flowLayout
    }


  2. Pintrest Layout :
    https://www.raywenderlich.com/392-uicollectionview-custom-layout-tutorial-pinterest



For Example : when user Click on Profile Button FlowLayout will be Apllied and Cell Appear with image in Circle Shape. when user click on Picture button pintrest layout will be Applied and cell Appear with image in Rectangle shape with dynamic height.



enter image description here



intially CollectionView have 1.flowLayout and it appears perfectly.but when I click on Picture button Pintrest layout is messed up with previous layout as shown in above image.



Following is Code For changing Layout.



if isGrid {
let horizontal = flowLayout
recentCollectionView.setCollectionViewLayout(horizontal, animated: true)
recentCollectionView.reloadData()
}
else {
let horizontal = PinterestLayout()
horizontal.delegate = self
recentCollectionView.setCollectionViewLayout(horizontal, animated: true)
recentCollectionView.reloadData()
}


ViewHiarchy:



I have main Collection-view that Contain header and one bottom cell.cell contain other Collection-view to which I am Applying multiple layout.I have Two Different cell for each layout.I want bottom cell size equal to content Collection-view Content size so user can Scroll entire main collection-view vertically.



 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell : UICollectionViewCell!

switch isGrid {
case true:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SearchProfileCell", for: indexPath)
if let annotateCell = cell as? SearchProfileCell {
annotateCell.photo = photos[indexPath.item]
}
case false:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AnnotatedPhotoCell", for: indexPath)

if let annotateCell = cell as? AnnotatedPhotoCell {
annotateCell.cellwidth = collectionView.contentSize.width/3
annotateCell.photo = photos[indexPath.item]

}
}

cell.contentView.layer.cornerRadius = 0
return cell
}


Code of profile and picture button Action.



@IBAction func pictureClick(sender:UIButton) {
isGrid = false
self.searchCollectionView.reloadData()
}

@IBAction func profilClick(sender:UIButton) {
isGrid = true
self.searchCollectionView.reloadData()
}






ios swift uicollectionview uicollectionviewlayout






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 9:40

























asked Nov 13 at 5:28









Bhavesh.iosDev

338111




338111












  • try performBatchUpdates
    – Prashant Tukadiya
    Nov 13 at 5:39










  • do you mean instead of reloadData i should use PerformBatchUpdate right?
    – Bhavesh.iosDev
    Nov 13 at 6:24










  • Inside block of performBatchUpdate put your layout reset code
    – Prashant Tukadiya
    Nov 13 at 6:30










  • okay.let me try
    – Bhavesh.iosDev
    Nov 13 at 6:34










  • I would think you should use layoutSubviews instead of reloadData() so after your IF Statement try self.view.layoutSubviews. You want to trigger a redraw of the view controller
    – Alexander
    Nov 13 at 7:49




















  • try performBatchUpdates
    – Prashant Tukadiya
    Nov 13 at 5:39










  • do you mean instead of reloadData i should use PerformBatchUpdate right?
    – Bhavesh.iosDev
    Nov 13 at 6:24










  • Inside block of performBatchUpdate put your layout reset code
    – Prashant Tukadiya
    Nov 13 at 6:30










  • okay.let me try
    – Bhavesh.iosDev
    Nov 13 at 6:34










  • I would think you should use layoutSubviews instead of reloadData() so after your IF Statement try self.view.layoutSubviews. You want to trigger a redraw of the view controller
    – Alexander
    Nov 13 at 7:49


















try performBatchUpdates
– Prashant Tukadiya
Nov 13 at 5:39




try performBatchUpdates
– Prashant Tukadiya
Nov 13 at 5:39












do you mean instead of reloadData i should use PerformBatchUpdate right?
– Bhavesh.iosDev
Nov 13 at 6:24




do you mean instead of reloadData i should use PerformBatchUpdate right?
– Bhavesh.iosDev
Nov 13 at 6:24












Inside block of performBatchUpdate put your layout reset code
– Prashant Tukadiya
Nov 13 at 6:30




Inside block of performBatchUpdate put your layout reset code
– Prashant Tukadiya
Nov 13 at 6:30












okay.let me try
– Bhavesh.iosDev
Nov 13 at 6:34




okay.let me try
– Bhavesh.iosDev
Nov 13 at 6:34












I would think you should use layoutSubviews instead of reloadData() so after your IF Statement try self.view.layoutSubviews. You want to trigger a redraw of the view controller
– Alexander
Nov 13 at 7:49






I would think you should use layoutSubviews instead of reloadData() so after your IF Statement try self.view.layoutSubviews. You want to trigger a redraw of the view controller
– Alexander
Nov 13 at 7:49














2 Answers
2






active

oldest

votes


















1














Why you are using two different layout even though you can achieve same Result with pintrestLayout. https://www.raywenderlich.com/392-uicollectionview-custom-layout-tutorial-pinterest.



Check pintrestLayout carefully , it have Delegate for Dynamic height.



 let photoHeight = delegate.collectionView(collectionView, heightForPhotoAtIndexPath: indexPath)


if you return static height here , your pintrest layout become GridLayout(your First Layout).



if you want pintrest layout as work for both layout , you need to declare same Boolean(isGrid) in pintrest layout.and use this boolean to return UICollectionViewLayoutAttributes



more important raywenderlich pintrest layout uses cache to store layout attribute.you have to remove cache object before applying other layout.



Check in this tutorial , how same layout used for grid,list and linear.
https://benoitpasquier.com/optimise-uicollectionview-swift/



what you need in your layout.



 var isGrid : Bool = true {
didSet {
if isGrid != oldValue {
cache.removeAll()
self.invalidateLayout()
}
}
}





share|improve this answer























  • hi could you please give some code example that illustrate how single layout work for diffrent design.
    – Bhavesh.iosDev
    Nov 15 at 10:50










  • i have updated my answer please check onece
    – kokila
    Nov 15 at 13:20



















2














I think problem is not inside layout but might be inside cellForItemAt. if you are using different cell for both layout then do not compare bool at cellForItemAt method. you should compare layout class type
like below code :



 func  collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView.collectionViewLayout.isKind(of: PinterestLayout.self) {
// return cell for PinterestLayout
guard let annotateCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SearchProfileCell", for: indexPath) as? SearchProfileCell else {
fatalError("SearchProfileCell Not Found")
}
annotateCell.photo = photos[indexPath.item]
return annotateCell
} else {
// return cell for flowLayout
guard let annotateCell = collectionView.dequeueReusableCell(withReuseIdentifier: "AnnotatedPhotoCell", for: indexPath) as? AnnotatedPhotoCell else {
fatalError("AnnotatedPhotoCell Not Found")
}
annotateCell.cellwidth = collectionView.contentSize.width/3
annotateCell.photo = photos[indexPath.item]
return annotateCell
}
}


Also need to update layout change action methods like:



     @IBAction func pictureClick(sender:UIButton) {
isGrid = false

self.collectionView?.collectionViewLayout.invalidateLayout()
self.collectionView?.setCollectionViewLayout(PinterestLayout(),
animated: false, completion: { [weak self] (complite) in
guard let strongSelf = self else {
return
}
strongSelf.searchCollectionView?.reloadData()
})
}

@IBAction func profilClick(sender:UIButton) {
isGrid = true
self.collectionView?.collectionViewLayout.invalidateLayout()
self.collectionView?.setCollectionViewLayout(flowLayout,
animated: false, completion: { [weak self] (complite) in
guard let strongSelf = self else {
return
}
strongSelf.searchCollectionView?.reloadData()
})
}





share|improve this answer























  • thanks for suggestion.i have replaced your code of cellforindexpath, but still have same problem.
    – Bhavesh.iosDev
    Nov 14 at 9:34










  • Can you try setCollectionViewLayout with completion block?
    – Jatin Kathrotiya
    Nov 14 at 11:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274380%2fsingle-collectionview-with-multiple-collectionviewlayout-why-layout-is-mess-up%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Why you are using two different layout even though you can achieve same Result with pintrestLayout. https://www.raywenderlich.com/392-uicollectionview-custom-layout-tutorial-pinterest.



Check pintrestLayout carefully , it have Delegate for Dynamic height.



 let photoHeight = delegate.collectionView(collectionView, heightForPhotoAtIndexPath: indexPath)


if you return static height here , your pintrest layout become GridLayout(your First Layout).



if you want pintrest layout as work for both layout , you need to declare same Boolean(isGrid) in pintrest layout.and use this boolean to return UICollectionViewLayoutAttributes



more important raywenderlich pintrest layout uses cache to store layout attribute.you have to remove cache object before applying other layout.



Check in this tutorial , how same layout used for grid,list and linear.
https://benoitpasquier.com/optimise-uicollectionview-swift/



what you need in your layout.



 var isGrid : Bool = true {
didSet {
if isGrid != oldValue {
cache.removeAll()
self.invalidateLayout()
}
}
}





share|improve this answer























  • hi could you please give some code example that illustrate how single layout work for diffrent design.
    – Bhavesh.iosDev
    Nov 15 at 10:50










  • i have updated my answer please check onece
    – kokila
    Nov 15 at 13:20
















1














Why you are using two different layout even though you can achieve same Result with pintrestLayout. https://www.raywenderlich.com/392-uicollectionview-custom-layout-tutorial-pinterest.



Check pintrestLayout carefully , it have Delegate for Dynamic height.



 let photoHeight = delegate.collectionView(collectionView, heightForPhotoAtIndexPath: indexPath)


if you return static height here , your pintrest layout become GridLayout(your First Layout).



if you want pintrest layout as work for both layout , you need to declare same Boolean(isGrid) in pintrest layout.and use this boolean to return UICollectionViewLayoutAttributes



more important raywenderlich pintrest layout uses cache to store layout attribute.you have to remove cache object before applying other layout.



Check in this tutorial , how same layout used for grid,list and linear.
https://benoitpasquier.com/optimise-uicollectionview-swift/



what you need in your layout.



 var isGrid : Bool = true {
didSet {
if isGrid != oldValue {
cache.removeAll()
self.invalidateLayout()
}
}
}





share|improve this answer























  • hi could you please give some code example that illustrate how single layout work for diffrent design.
    – Bhavesh.iosDev
    Nov 15 at 10:50










  • i have updated my answer please check onece
    – kokila
    Nov 15 at 13:20














1












1








1






Why you are using two different layout even though you can achieve same Result with pintrestLayout. https://www.raywenderlich.com/392-uicollectionview-custom-layout-tutorial-pinterest.



Check pintrestLayout carefully , it have Delegate for Dynamic height.



 let photoHeight = delegate.collectionView(collectionView, heightForPhotoAtIndexPath: indexPath)


if you return static height here , your pintrest layout become GridLayout(your First Layout).



if you want pintrest layout as work for both layout , you need to declare same Boolean(isGrid) in pintrest layout.and use this boolean to return UICollectionViewLayoutAttributes



more important raywenderlich pintrest layout uses cache to store layout attribute.you have to remove cache object before applying other layout.



Check in this tutorial , how same layout used for grid,list and linear.
https://benoitpasquier.com/optimise-uicollectionview-swift/



what you need in your layout.



 var isGrid : Bool = true {
didSet {
if isGrid != oldValue {
cache.removeAll()
self.invalidateLayout()
}
}
}





share|improve this answer














Why you are using two different layout even though you can achieve same Result with pintrestLayout. https://www.raywenderlich.com/392-uicollectionview-custom-layout-tutorial-pinterest.



Check pintrestLayout carefully , it have Delegate for Dynamic height.



 let photoHeight = delegate.collectionView(collectionView, heightForPhotoAtIndexPath: indexPath)


if you return static height here , your pintrest layout become GridLayout(your First Layout).



if you want pintrest layout as work for both layout , you need to declare same Boolean(isGrid) in pintrest layout.and use this boolean to return UICollectionViewLayoutAttributes



more important raywenderlich pintrest layout uses cache to store layout attribute.you have to remove cache object before applying other layout.



Check in this tutorial , how same layout used for grid,list and linear.
https://benoitpasquier.com/optimise-uicollectionview-swift/



what you need in your layout.



 var isGrid : Bool = true {
didSet {
if isGrid != oldValue {
cache.removeAll()
self.invalidateLayout()
}
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 at 13:20

























answered Nov 15 at 7:00









kokila

263




263












  • hi could you please give some code example that illustrate how single layout work for diffrent design.
    – Bhavesh.iosDev
    Nov 15 at 10:50










  • i have updated my answer please check onece
    – kokila
    Nov 15 at 13:20


















  • hi could you please give some code example that illustrate how single layout work for diffrent design.
    – Bhavesh.iosDev
    Nov 15 at 10:50










  • i have updated my answer please check onece
    – kokila
    Nov 15 at 13:20
















hi could you please give some code example that illustrate how single layout work for diffrent design.
– Bhavesh.iosDev
Nov 15 at 10:50




hi could you please give some code example that illustrate how single layout work for diffrent design.
– Bhavesh.iosDev
Nov 15 at 10:50












i have updated my answer please check onece
– kokila
Nov 15 at 13:20




i have updated my answer please check onece
– kokila
Nov 15 at 13:20













2














I think problem is not inside layout but might be inside cellForItemAt. if you are using different cell for both layout then do not compare bool at cellForItemAt method. you should compare layout class type
like below code :



 func  collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView.collectionViewLayout.isKind(of: PinterestLayout.self) {
// return cell for PinterestLayout
guard let annotateCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SearchProfileCell", for: indexPath) as? SearchProfileCell else {
fatalError("SearchProfileCell Not Found")
}
annotateCell.photo = photos[indexPath.item]
return annotateCell
} else {
// return cell for flowLayout
guard let annotateCell = collectionView.dequeueReusableCell(withReuseIdentifier: "AnnotatedPhotoCell", for: indexPath) as? AnnotatedPhotoCell else {
fatalError("AnnotatedPhotoCell Not Found")
}
annotateCell.cellwidth = collectionView.contentSize.width/3
annotateCell.photo = photos[indexPath.item]
return annotateCell
}
}


Also need to update layout change action methods like:



     @IBAction func pictureClick(sender:UIButton) {
isGrid = false

self.collectionView?.collectionViewLayout.invalidateLayout()
self.collectionView?.setCollectionViewLayout(PinterestLayout(),
animated: false, completion: { [weak self] (complite) in
guard let strongSelf = self else {
return
}
strongSelf.searchCollectionView?.reloadData()
})
}

@IBAction func profilClick(sender:UIButton) {
isGrid = true
self.collectionView?.collectionViewLayout.invalidateLayout()
self.collectionView?.setCollectionViewLayout(flowLayout,
animated: false, completion: { [weak self] (complite) in
guard let strongSelf = self else {
return
}
strongSelf.searchCollectionView?.reloadData()
})
}





share|improve this answer























  • thanks for suggestion.i have replaced your code of cellforindexpath, but still have same problem.
    – Bhavesh.iosDev
    Nov 14 at 9:34










  • Can you try setCollectionViewLayout with completion block?
    – Jatin Kathrotiya
    Nov 14 at 11:46
















2














I think problem is not inside layout but might be inside cellForItemAt. if you are using different cell for both layout then do not compare bool at cellForItemAt method. you should compare layout class type
like below code :



 func  collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView.collectionViewLayout.isKind(of: PinterestLayout.self) {
// return cell for PinterestLayout
guard let annotateCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SearchProfileCell", for: indexPath) as? SearchProfileCell else {
fatalError("SearchProfileCell Not Found")
}
annotateCell.photo = photos[indexPath.item]
return annotateCell
} else {
// return cell for flowLayout
guard let annotateCell = collectionView.dequeueReusableCell(withReuseIdentifier: "AnnotatedPhotoCell", for: indexPath) as? AnnotatedPhotoCell else {
fatalError("AnnotatedPhotoCell Not Found")
}
annotateCell.cellwidth = collectionView.contentSize.width/3
annotateCell.photo = photos[indexPath.item]
return annotateCell
}
}


Also need to update layout change action methods like:



     @IBAction func pictureClick(sender:UIButton) {
isGrid = false

self.collectionView?.collectionViewLayout.invalidateLayout()
self.collectionView?.setCollectionViewLayout(PinterestLayout(),
animated: false, completion: { [weak self] (complite) in
guard let strongSelf = self else {
return
}
strongSelf.searchCollectionView?.reloadData()
})
}

@IBAction func profilClick(sender:UIButton) {
isGrid = true
self.collectionView?.collectionViewLayout.invalidateLayout()
self.collectionView?.setCollectionViewLayout(flowLayout,
animated: false, completion: { [weak self] (complite) in
guard let strongSelf = self else {
return
}
strongSelf.searchCollectionView?.reloadData()
})
}





share|improve this answer























  • thanks for suggestion.i have replaced your code of cellforindexpath, but still have same problem.
    – Bhavesh.iosDev
    Nov 14 at 9:34










  • Can you try setCollectionViewLayout with completion block?
    – Jatin Kathrotiya
    Nov 14 at 11:46














2












2








2






I think problem is not inside layout but might be inside cellForItemAt. if you are using different cell for both layout then do not compare bool at cellForItemAt method. you should compare layout class type
like below code :



 func  collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView.collectionViewLayout.isKind(of: PinterestLayout.self) {
// return cell for PinterestLayout
guard let annotateCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SearchProfileCell", for: indexPath) as? SearchProfileCell else {
fatalError("SearchProfileCell Not Found")
}
annotateCell.photo = photos[indexPath.item]
return annotateCell
} else {
// return cell for flowLayout
guard let annotateCell = collectionView.dequeueReusableCell(withReuseIdentifier: "AnnotatedPhotoCell", for: indexPath) as? AnnotatedPhotoCell else {
fatalError("AnnotatedPhotoCell Not Found")
}
annotateCell.cellwidth = collectionView.contentSize.width/3
annotateCell.photo = photos[indexPath.item]
return annotateCell
}
}


Also need to update layout change action methods like:



     @IBAction func pictureClick(sender:UIButton) {
isGrid = false

self.collectionView?.collectionViewLayout.invalidateLayout()
self.collectionView?.setCollectionViewLayout(PinterestLayout(),
animated: false, completion: { [weak self] (complite) in
guard let strongSelf = self else {
return
}
strongSelf.searchCollectionView?.reloadData()
})
}

@IBAction func profilClick(sender:UIButton) {
isGrid = true
self.collectionView?.collectionViewLayout.invalidateLayout()
self.collectionView?.setCollectionViewLayout(flowLayout,
animated: false, completion: { [weak self] (complite) in
guard let strongSelf = self else {
return
}
strongSelf.searchCollectionView?.reloadData()
})
}





share|improve this answer














I think problem is not inside layout but might be inside cellForItemAt. if you are using different cell for both layout then do not compare bool at cellForItemAt method. you should compare layout class type
like below code :



 func  collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView.collectionViewLayout.isKind(of: PinterestLayout.self) {
// return cell for PinterestLayout
guard let annotateCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SearchProfileCell", for: indexPath) as? SearchProfileCell else {
fatalError("SearchProfileCell Not Found")
}
annotateCell.photo = photos[indexPath.item]
return annotateCell
} else {
// return cell for flowLayout
guard let annotateCell = collectionView.dequeueReusableCell(withReuseIdentifier: "AnnotatedPhotoCell", for: indexPath) as? AnnotatedPhotoCell else {
fatalError("AnnotatedPhotoCell Not Found")
}
annotateCell.cellwidth = collectionView.contentSize.width/3
annotateCell.photo = photos[indexPath.item]
return annotateCell
}
}


Also need to update layout change action methods like:



     @IBAction func pictureClick(sender:UIButton) {
isGrid = false

self.collectionView?.collectionViewLayout.invalidateLayout()
self.collectionView?.setCollectionViewLayout(PinterestLayout(),
animated: false, completion: { [weak self] (complite) in
guard let strongSelf = self else {
return
}
strongSelf.searchCollectionView?.reloadData()
})
}

@IBAction func profilClick(sender:UIButton) {
isGrid = true
self.collectionView?.collectionViewLayout.invalidateLayout()
self.collectionView?.setCollectionViewLayout(flowLayout,
animated: false, completion: { [weak self] (complite) in
guard let strongSelf = self else {
return
}
strongSelf.searchCollectionView?.reloadData()
})
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 at 11:55

























answered Nov 13 at 9:01









Jatin Kathrotiya

389110




389110












  • thanks for suggestion.i have replaced your code of cellforindexpath, but still have same problem.
    – Bhavesh.iosDev
    Nov 14 at 9:34










  • Can you try setCollectionViewLayout with completion block?
    – Jatin Kathrotiya
    Nov 14 at 11:46


















  • thanks for suggestion.i have replaced your code of cellforindexpath, but still have same problem.
    – Bhavesh.iosDev
    Nov 14 at 9:34










  • Can you try setCollectionViewLayout with completion block?
    – Jatin Kathrotiya
    Nov 14 at 11:46
















thanks for suggestion.i have replaced your code of cellforindexpath, but still have same problem.
– Bhavesh.iosDev
Nov 14 at 9:34




thanks for suggestion.i have replaced your code of cellforindexpath, but still have same problem.
– Bhavesh.iosDev
Nov 14 at 9:34












Can you try setCollectionViewLayout with completion block?
– Jatin Kathrotiya
Nov 14 at 11:46




Can you try setCollectionViewLayout with completion block?
– Jatin Kathrotiya
Nov 14 at 11:46


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274380%2fsingle-collectionview-with-multiple-collectionviewlayout-why-layout-is-mess-up%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?