iosswifttaglist

TagListView single selection swift 4


I try to create tag using cocoa pod of TagListView https://github.com/ElaWorkshop/TagListView. I use TagListViewDelegate to receive tag pressed event and used isSelected property. For unselected tag i set as orange color, while selected tag as white color. Each time user click unselected tag, the tag will turn to white color (become selected). For the default, user can select multiple tags. What i want to achieve is, user only can select one tag (turn to white color) at once. It means, other tags will remain unselected (turn to orange color). Below is the code on what i had already did:

    func tagPressed(_ title: String, tagView: TagView, sender: TagListView) {
   // print("Tag pressed: \(title), \(sender)")

    if tagView.isSelected == false {
        tagView.isSelected = true
    }

}

Solution

  • there is more than solution this fast and easy one without update original TagListView classes to support one selection

           // MARK: TagListViewDelegate
                func tagPressed(_ title: String, tagView: TagView, sender: TagListView) 
          {
                    print("Tag pressed: \(title), \(sender)")
    
                    // loop over all tags and set selected to false
    
                     sender.tagViews.forEach {$0.isSelected = false}
    
                    tagView.isSelected = !tagView.isSelected
         }