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;
}







0















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:



Custom CollectionView cell image example










share|improve this question































    0















    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:



    Custom CollectionView cell image example










    share|improve this question



























      0












      0








      0








      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:



      Custom CollectionView cell image example










      share|improve this question
















      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:



      Custom CollectionView cell image example







      ios swift uiview uicollectionview uicollectionviewcell






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 21:41







      Newsonic

















      asked Nov 23 '18 at 21:04









      NewsonicNewsonic

      636




      636
























          2 Answers
          2






          active

          oldest

          votes


















          2














          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.






          share|improve this answer































            2














            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?






            share|improve this answer
























            • 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












            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%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









            2














            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.






            share|improve this answer




























              2














              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.






              share|improve this answer


























                2












                2








                2







                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.






                share|improve this answer













                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 21:51









                VyacheslavVyacheslav

                15.1k964127




                15.1k964127

























                    2














                    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?






                    share|improve this answer
























                    • 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
















                    2














                    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?






                    share|improve this answer
























                    • 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














                    2












                    2








                    2







                    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?






                    share|improve this answer













                    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?







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    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



















                    • 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


















                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()