How can we reload header view inside our collection view












1















I've been looking for a way to reload my collection view header. So I have a collection view header & a CollectionViewCell that only contains an image. Now when the cell is press, I would like to display the image in the header view without calling collectionView.reloadData(). This is how my didSelectItemAt & didDeselectItemAt method looks like.



override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedImage = images[indexPath.item]
let imageCell = collectionView.cellForItem(at: indexPath) as! CollectionCell
imageCell.photoBackgroundView.backgroundColor = .red

collectionView.reloadData()
}

override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let imageCell = collectionView.cellForItem(at: indexPath) as! ImagePickerCell
imageCell.photoBackgroundView.backgroundColor = .black
}


So when I select a cell the view turns red, when I deselect it the view turns black. This video here, shows how the behavior without reloading the collectionView. Now here is were I would like to reload the header view.



If I do use collectionView.reloadData(), this is the outcome. How would I be able to reload the header or the collectionView where the header view displays the selected cell image & turns red.










share|improve this question



























    1















    I've been looking for a way to reload my collection view header. So I have a collection view header & a CollectionViewCell that only contains an image. Now when the cell is press, I would like to display the image in the header view without calling collectionView.reloadData(). This is how my didSelectItemAt & didDeselectItemAt method looks like.



    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    selectedImage = images[indexPath.item]
    let imageCell = collectionView.cellForItem(at: indexPath) as! CollectionCell
    imageCell.photoBackgroundView.backgroundColor = .red

    collectionView.reloadData()
    }

    override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    let imageCell = collectionView.cellForItem(at: indexPath) as! ImagePickerCell
    imageCell.photoBackgroundView.backgroundColor = .black
    }


    So when I select a cell the view turns red, when I deselect it the view turns black. This video here, shows how the behavior without reloading the collectionView. Now here is were I would like to reload the header view.



    If I do use collectionView.reloadData(), this is the outcome. How would I be able to reload the header or the collectionView where the header view displays the selected cell image & turns red.










    share|improve this question

























      1












      1








      1








      I've been looking for a way to reload my collection view header. So I have a collection view header & a CollectionViewCell that only contains an image. Now when the cell is press, I would like to display the image in the header view without calling collectionView.reloadData(). This is how my didSelectItemAt & didDeselectItemAt method looks like.



      override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
      selectedImage = images[indexPath.item]
      let imageCell = collectionView.cellForItem(at: indexPath) as! CollectionCell
      imageCell.photoBackgroundView.backgroundColor = .red

      collectionView.reloadData()
      }

      override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
      let imageCell = collectionView.cellForItem(at: indexPath) as! ImagePickerCell
      imageCell.photoBackgroundView.backgroundColor = .black
      }


      So when I select a cell the view turns red, when I deselect it the view turns black. This video here, shows how the behavior without reloading the collectionView. Now here is were I would like to reload the header view.



      If I do use collectionView.reloadData(), this is the outcome. How would I be able to reload the header or the collectionView where the header view displays the selected cell image & turns red.










      share|improve this question














      I've been looking for a way to reload my collection view header. So I have a collection view header & a CollectionViewCell that only contains an image. Now when the cell is press, I would like to display the image in the header view without calling collectionView.reloadData(). This is how my didSelectItemAt & didDeselectItemAt method looks like.



      override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
      selectedImage = images[indexPath.item]
      let imageCell = collectionView.cellForItem(at: indexPath) as! CollectionCell
      imageCell.photoBackgroundView.backgroundColor = .red

      collectionView.reloadData()
      }

      override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
      let imageCell = collectionView.cellForItem(at: indexPath) as! ImagePickerCell
      imageCell.photoBackgroundView.backgroundColor = .black
      }


      So when I select a cell the view turns red, when I deselect it the view turns black. This video here, shows how the behavior without reloading the collectionView. Now here is were I would like to reload the header view.



      If I do use collectionView.reloadData(), this is the outcome. How would I be able to reload the header or the collectionView where the header view displays the selected cell image & turns red.







      swift uicollectionview






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 '18 at 5:39









      Luis F RamirezLuis F Ramirez

      417




      417
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You can try like global instance for that. Like



          class YourClass: UIViewController {

          /// Profile imageView
          var profileImageview = UIImageView()

          }


          In CollectionView cellforItem assign a imageview. Like



          let imageCell = collectionView.cellForItem(at: indexPath) as! CollectionCell
          profileImageview = imageCell.imageView


          Then when every you selecting collectionViewCell



          You can call a function to change a image of imageView. Like



          func updateImage() {
          profileImageview.image = UIImage()

          }





          share|improve this answer































            0














            Well I'm trying to understand why sometimes you cast the cell in CollectionCell and sometimes in ImagePickerCell, anyway try to change your functions in these



            func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            let cell = collectionView.cellForItem(at: indexPath) as! CollectionCell
            cell?.photoBackgroundView.backgroundColor = .red
            }

            func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
            let cell = collectionView.cellForItem(at: indexPath) as! CollectionCell
            cell?.photoBackgroundView.backgroundColor = .black
            }





            share|improve this answer

























              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%2f53475278%2fhow-can-we-reload-header-view-inside-our-collection-view%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














              You can try like global instance for that. Like



              class YourClass: UIViewController {

              /// Profile imageView
              var profileImageview = UIImageView()

              }


              In CollectionView cellforItem assign a imageview. Like



              let imageCell = collectionView.cellForItem(at: indexPath) as! CollectionCell
              profileImageview = imageCell.imageView


              Then when every you selecting collectionViewCell



              You can call a function to change a image of imageView. Like



              func updateImage() {
              profileImageview.image = UIImage()

              }





              share|improve this answer




























                1














                You can try like global instance for that. Like



                class YourClass: UIViewController {

                /// Profile imageView
                var profileImageview = UIImageView()

                }


                In CollectionView cellforItem assign a imageview. Like



                let imageCell = collectionView.cellForItem(at: indexPath) as! CollectionCell
                profileImageview = imageCell.imageView


                Then when every you selecting collectionViewCell



                You can call a function to change a image of imageView. Like



                func updateImage() {
                profileImageview.image = UIImage()

                }





                share|improve this answer


























                  1












                  1








                  1







                  You can try like global instance for that. Like



                  class YourClass: UIViewController {

                  /// Profile imageView
                  var profileImageview = UIImageView()

                  }


                  In CollectionView cellforItem assign a imageview. Like



                  let imageCell = collectionView.cellForItem(at: indexPath) as! CollectionCell
                  profileImageview = imageCell.imageView


                  Then when every you selecting collectionViewCell



                  You can call a function to change a image of imageView. Like



                  func updateImage() {
                  profileImageview.image = UIImage()

                  }





                  share|improve this answer













                  You can try like global instance for that. Like



                  class YourClass: UIViewController {

                  /// Profile imageView
                  var profileImageview = UIImageView()

                  }


                  In CollectionView cellforItem assign a imageview. Like



                  let imageCell = collectionView.cellForItem(at: indexPath) as! CollectionCell
                  profileImageview = imageCell.imageView


                  Then when every you selecting collectionViewCell



                  You can call a function to change a image of imageView. Like



                  func updateImage() {
                  profileImageview.image = UIImage()

                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 6:12









                  ktr kathirktr kathir

                  912624




                  912624

























                      0














                      Well I'm trying to understand why sometimes you cast the cell in CollectionCell and sometimes in ImagePickerCell, anyway try to change your functions in these



                      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
                      let cell = collectionView.cellForItem(at: indexPath) as! CollectionCell
                      cell?.photoBackgroundView.backgroundColor = .red
                      }

                      func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
                      let cell = collectionView.cellForItem(at: indexPath) as! CollectionCell
                      cell?.photoBackgroundView.backgroundColor = .black
                      }





                      share|improve this answer






























                        0














                        Well I'm trying to understand why sometimes you cast the cell in CollectionCell and sometimes in ImagePickerCell, anyway try to change your functions in these



                        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
                        let cell = collectionView.cellForItem(at: indexPath) as! CollectionCell
                        cell?.photoBackgroundView.backgroundColor = .red
                        }

                        func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
                        let cell = collectionView.cellForItem(at: indexPath) as! CollectionCell
                        cell?.photoBackgroundView.backgroundColor = .black
                        }





                        share|improve this answer




























                          0












                          0








                          0







                          Well I'm trying to understand why sometimes you cast the cell in CollectionCell and sometimes in ImagePickerCell, anyway try to change your functions in these



                          func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
                          let cell = collectionView.cellForItem(at: indexPath) as! CollectionCell
                          cell?.photoBackgroundView.backgroundColor = .red
                          }

                          func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
                          let cell = collectionView.cellForItem(at: indexPath) as! CollectionCell
                          cell?.photoBackgroundView.backgroundColor = .black
                          }





                          share|improve this answer















                          Well I'm trying to understand why sometimes you cast the cell in CollectionCell and sometimes in ImagePickerCell, anyway try to change your functions in these



                          func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
                          let cell = collectionView.cellForItem(at: indexPath) as! CollectionCell
                          cell?.photoBackgroundView.backgroundColor = .red
                          }

                          func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
                          let cell = collectionView.cellForItem(at: indexPath) as! CollectionCell
                          cell?.photoBackgroundView.backgroundColor = .black
                          }






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 26 '18 at 11:00

























                          answered Nov 26 '18 at 10:50









                          Francesco DestinoFrancesco Destino

                          14112




                          14112






























                              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%2f53475278%2fhow-can-we-reload-header-view-inside-our-collection-view%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

                              Contact image not getting when fetch all contact list from iPhone by CNContact

                              count number of partitions of a set with n elements into k subsets

                              A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks