How to copy/duplicate a custom collectionview cell in Swift?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I want to copy a custom collectionview cell from a viewcontroller to another, the problem is the collectionview cell disappears once I tap on it because of - apparently - adding it in the 2nd viewcontroller's as a subview.
I tried most of the methods here Create a copy of a UIView in Swift
One about Archiving returns nil.
Other about prototypes and structs, it doesn't return a new address for the view.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
struct ObjectToCopied {
var objToBeRetrieved: UIView? = nil
init(objToSaved : UIView?) {
objToBeRetrieved = objToSaved
}
}
let pickedView : UIView? = collectionView.cellForItem(at: indexPath)
let tempObj = ObjectToCopied(objToSaved: pickedView)
let copiedObj = tempObj
let copiedView = copiedObj.objToBeRetrieved as? UIView
// here the copiedview's address is THE SAME as tempObj's ObjToBeRetrieved.
}
Custom CollectionView cell image example:
ios swift uiview uicollectionview uicollectionviewcell
add a comment |
I want to copy a custom collectionview cell from a viewcontroller to another, the problem is the collectionview cell disappears once I tap on it because of - apparently - adding it in the 2nd viewcontroller's as a subview.
I tried most of the methods here Create a copy of a UIView in Swift
One about Archiving returns nil.
Other about prototypes and structs, it doesn't return a new address for the view.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
struct ObjectToCopied {
var objToBeRetrieved: UIView? = nil
init(objToSaved : UIView?) {
objToBeRetrieved = objToSaved
}
}
let pickedView : UIView? = collectionView.cellForItem(at: indexPath)
let tempObj = ObjectToCopied(objToSaved: pickedView)
let copiedObj = tempObj
let copiedView = copiedObj.objToBeRetrieved as? UIView
// here the copiedview's address is THE SAME as tempObj's ObjToBeRetrieved.
}
Custom CollectionView cell image example:
ios swift uiview uicollectionview uicollectionviewcell
add a comment |
I want to copy a custom collectionview cell from a viewcontroller to another, the problem is the collectionview cell disappears once I tap on it because of - apparently - adding it in the 2nd viewcontroller's as a subview.
I tried most of the methods here Create a copy of a UIView in Swift
One about Archiving returns nil.
Other about prototypes and structs, it doesn't return a new address for the view.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
struct ObjectToCopied {
var objToBeRetrieved: UIView? = nil
init(objToSaved : UIView?) {
objToBeRetrieved = objToSaved
}
}
let pickedView : UIView? = collectionView.cellForItem(at: indexPath)
let tempObj = ObjectToCopied(objToSaved: pickedView)
let copiedObj = tempObj
let copiedView = copiedObj.objToBeRetrieved as? UIView
// here the copiedview's address is THE SAME as tempObj's ObjToBeRetrieved.
}
Custom CollectionView cell image example:
ios swift uiview uicollectionview uicollectionviewcell
I want to copy a custom collectionview cell from a viewcontroller to another, the problem is the collectionview cell disappears once I tap on it because of - apparently - adding it in the 2nd viewcontroller's as a subview.
I tried most of the methods here Create a copy of a UIView in Swift
One about Archiving returns nil.
Other about prototypes and structs, it doesn't return a new address for the view.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
struct ObjectToCopied {
var objToBeRetrieved: UIView? = nil
init(objToSaved : UIView?) {
objToBeRetrieved = objToSaved
}
}
let pickedView : UIView? = collectionView.cellForItem(at: indexPath)
let tempObj = ObjectToCopied(objToSaved: pickedView)
let copiedObj = tempObj
let copiedView = copiedObj.objToBeRetrieved as? UIView
// here the copiedview's address is THE SAME as tempObj's ObjToBeRetrieved.
}
Custom CollectionView cell image example:
ios swift uiview uicollectionview uicollectionviewcell
ios swift uiview uicollectionview uicollectionviewcell
edited Nov 23 '18 at 21:41
Newsonic
asked Nov 23 '18 at 21:04
NewsonicNewsonic
636
636
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Don't copy UIView object!
Your model isn't good.
You have to store the data – information somewhere outside the cell directly.
You have to call your 'database' using indexPath
to retrieve the information. No more!
About your question.
This object doesn't copy your object.
struct ObjectToCopied {
var objToBeRetrieved: UIView? = nil
init(objToSaved : UIView?) {
objToBeRetrieved = objToSaved
}
}
ObjectToCopied
is a value type. But UIView
is a reference type. So, objToBeRetrieved contains the same address as pickedView
. Solution? Use objToBeRetrieved = objToSaved.copy()
. But, please, really don't do it. That's really bad. Futhermore, I think this will corrupt something. Use better application model – the first suggestion.
add a comment |
Why do you need to copy the collectionView cell? You can simply pass your data to next viewController and create new view or cell (whatever you need) and display data on it.
Any specific reason to copy collectionView cell?
Nope, just wanted to copy it in few lines of code instead of the hassle of creating it again
– Newsonic
Nov 24 '18 at 15:15
It's not a good way to copy UI object.
– iDev750
Nov 24 '18 at 17:19
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452925%2fhow-to-copy-duplicate-a-custom-collectionview-cell-in-swift%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
Don't copy UIView object!
Your model isn't good.
You have to store the data – information somewhere outside the cell directly.
You have to call your 'database' using indexPath
to retrieve the information. No more!
About your question.
This object doesn't copy your object.
struct ObjectToCopied {
var objToBeRetrieved: UIView? = nil
init(objToSaved : UIView?) {
objToBeRetrieved = objToSaved
}
}
ObjectToCopied
is a value type. But UIView
is a reference type. So, objToBeRetrieved contains the same address as pickedView
. Solution? Use objToBeRetrieved = objToSaved.copy()
. But, please, really don't do it. That's really bad. Futhermore, I think this will corrupt something. Use better application model – the first suggestion.
add a comment |
Don't copy UIView object!
Your model isn't good.
You have to store the data – information somewhere outside the cell directly.
You have to call your 'database' using indexPath
to retrieve the information. No more!
About your question.
This object doesn't copy your object.
struct ObjectToCopied {
var objToBeRetrieved: UIView? = nil
init(objToSaved : UIView?) {
objToBeRetrieved = objToSaved
}
}
ObjectToCopied
is a value type. But UIView
is a reference type. So, objToBeRetrieved contains the same address as pickedView
. Solution? Use objToBeRetrieved = objToSaved.copy()
. But, please, really don't do it. That's really bad. Futhermore, I think this will corrupt something. Use better application model – the first suggestion.
add a comment |
Don't copy UIView object!
Your model isn't good.
You have to store the data – information somewhere outside the cell directly.
You have to call your 'database' using indexPath
to retrieve the information. No more!
About your question.
This object doesn't copy your object.
struct ObjectToCopied {
var objToBeRetrieved: UIView? = nil
init(objToSaved : UIView?) {
objToBeRetrieved = objToSaved
}
}
ObjectToCopied
is a value type. But UIView
is a reference type. So, objToBeRetrieved contains the same address as pickedView
. Solution? Use objToBeRetrieved = objToSaved.copy()
. But, please, really don't do it. That's really bad. Futhermore, I think this will corrupt something. Use better application model – the first suggestion.
Don't copy UIView object!
Your model isn't good.
You have to store the data – information somewhere outside the cell directly.
You have to call your 'database' using indexPath
to retrieve the information. No more!
About your question.
This object doesn't copy your object.
struct ObjectToCopied {
var objToBeRetrieved: UIView? = nil
init(objToSaved : UIView?) {
objToBeRetrieved = objToSaved
}
}
ObjectToCopied
is a value type. But UIView
is a reference type. So, objToBeRetrieved contains the same address as pickedView
. Solution? Use objToBeRetrieved = objToSaved.copy()
. But, please, really don't do it. That's really bad. Futhermore, I think this will corrupt something. Use better application model – the first suggestion.
answered Nov 23 '18 at 21:51
VyacheslavVyacheslav
15.1k964127
15.1k964127
add a comment |
add a comment |
Why do you need to copy the collectionView cell? You can simply pass your data to next viewController and create new view or cell (whatever you need) and display data on it.
Any specific reason to copy collectionView cell?
Nope, just wanted to copy it in few lines of code instead of the hassle of creating it again
– Newsonic
Nov 24 '18 at 15:15
It's not a good way to copy UI object.
– iDev750
Nov 24 '18 at 17:19
add a comment |
Why do you need to copy the collectionView cell? You can simply pass your data to next viewController and create new view or cell (whatever you need) and display data on it.
Any specific reason to copy collectionView cell?
Nope, just wanted to copy it in few lines of code instead of the hassle of creating it again
– Newsonic
Nov 24 '18 at 15:15
It's not a good way to copy UI object.
– iDev750
Nov 24 '18 at 17:19
add a comment |
Why do you need to copy the collectionView cell? You can simply pass your data to next viewController and create new view or cell (whatever you need) and display data on it.
Any specific reason to copy collectionView cell?
Why do you need to copy the collectionView cell? You can simply pass your data to next viewController and create new view or cell (whatever you need) and display data on it.
Any specific reason to copy collectionView cell?
answered Nov 24 '18 at 6:08
iDev750iDev750
6251417
6251417
Nope, just wanted to copy it in few lines of code instead of the hassle of creating it again
– Newsonic
Nov 24 '18 at 15:15
It's not a good way to copy UI object.
– iDev750
Nov 24 '18 at 17:19
add a comment |
Nope, just wanted to copy it in few lines of code instead of the hassle of creating it again
– Newsonic
Nov 24 '18 at 15:15
It's not a good way to copy UI object.
– iDev750
Nov 24 '18 at 17:19
Nope, just wanted to copy it in few lines of code instead of the hassle of creating it again
– Newsonic
Nov 24 '18 at 15:15
Nope, just wanted to copy it in few lines of code instead of the hassle of creating it again
– Newsonic
Nov 24 '18 at 15:15
It's not a good way to copy UI object.
– iDev750
Nov 24 '18 at 17:19
It's not a good way to copy UI object.
– iDev750
Nov 24 '18 at 17:19
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452925%2fhow-to-copy-duplicate-a-custom-collectionview-cell-in-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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