ios - How to handle multiple selection for a UICollectionView, which changes data on swipe? -


i have uicollectionview on uiviewcontroller, changes data on swipe, has search. change array values , reload uicollectionview every swipes. when tried implement multiple selection, selection changes on scrolling.

the code use is,

func collectionview(_ collectionview: uicollectionview, numberofitemsinsection section: int) -> int {          if searchison {             return filteredteamnames.count         }         else{             return teamnames.count         }         }   func collectionview(_ collectionview: uicollectionview, cellforitemat indexpath: indexpath) -> uicollectionviewcell {         let cell = collectionview.dequeuereusablecell(withreuseidentifier: reuseidentifiertwo, for: indexpath indexpath) as! housecells          cell.layer.maskstobounds = true         cell.layer.cornerradius = 6                      var singlecomponent = [string : string]()         if searchison {              singlecomponent = self.filteredteamnames[indexpath.item]         } else {              singlecomponent = self.teamnames[indexpath.item]         }         let stringurl: string = singlecomponent["icon_profile"]!          cell.teamimage.sd_setimage(with: url(string: stringurl), placeholderimage: uiimage(named: "demo"))         cell.teamnamelabel.text = singlecomponent["username"]          //to check bg color         let idvalue: int = int(singlecomponent["id"]!)!         print(idvalue)          if selectedteamids.contains(idvalue) {             cell.backgroundcolor = uicolor(red:0/255, green:0/255, blue:0/255, alpha:1.0)             cell.layer.borderwidth = 2.0             cell.layer.bordercolor = uicolor(red:56/255, green:192/255, blue:201/255, alpha:1.0).cgcolor          } else {             cell.backgroundcolor = uicolor(red:26/255, green:26/255, blue:26/255, alpha:0.4)          }          return cell  }  func collectionview(_ collectionview: uicollectionview, didselectitemat indexpath: indexpath) {         var singlecomponent = [string : string]()         if searchison {             singlecomponent = self.filteredteamnames[indexpath.item]          } else {             singlecomponent = self.teamnames[indexpath.item]          }         let idtempvalue: int = int(singlecomponent["id"]!)!         print(idtempvalue)          if !selectedteamids.contains(idtempvalue) {             selectedteamids.append(idtempvalue)          }         print(selectedteamids)          middlesectioncollview.reloaddata()      } 

the array selectedteamids contains unique numbers, if user clicks on particular index, i'd added corresponding id selectedteamids array , in cellforitemat ,i'm checking if current index value's id exists in selectedteamids array , if yes i'm changing background color of cell. code isn't working, selection changes on scrolling. missing logic?

first dont reloaddata inside didselectitemat method, remove reload line

second in custom cell class housecells (by way name of class should singular)

add code

  override var isselected: bool {         didset {              if isselected {                 backgroundcolor = uicolor(red:0/255, green:0/255, blue:0/255, alpha:1.0)                 layer.borderwidth = 2.0                 layer.bordercolor = uicolor(red:56/255, green:192/255, blue:201/255, alpha:1.0).cgcolor              } else {                 backgroundcolor = uicolor(red:26/255, green:26/255, blue:26/255, alpha:0.4)              }           }     } 

also remove code cellforitemat method

if selectedteamids.contains(idvalue) {             cell.backgroundcolor = uicolor(red:0/255, green:0/255, blue:0/255, alpha:1.0)             cell.layer.borderwidth = 2.0             cell.layer.bordercolor = uicolor(red:56/255, green:192/255, blue:201/255, alpha:1.0).cgcolor          } else {             cell.backgroundcolor = uicolor(red:26/255, green:26/255, blue:26/255, alpha:0.4)          } 

and in didselectitemat method dont need code now:

if !selectedteamids.contains(idtempvalue) {             selectedteamids.append(idtempvalue)          }         print(selectedteamids)          middlesectioncollview.reloaddata() 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -