Search code examples
iosswifttaglist

Perform action in TagViews inside UITableViewCell


I am using TaglistView to show tags in UITableViewCell. https://github.com/ElaWorkshop/TagListView

Now need to get id for the tag selected on a particular index path.

eg: JSON array is in format:

Index 0 : [{ name: "abc", value: "11" },{ name: "c", value: "12"}]

Index 1 : [{ name: "abcd", value: "21" },{ name: "abcde", value: "22" }]

Index 2 : [{ name: "abcde", value: "31" }, { name: "abcde", value: "32" }]

Code that I have used for adding taglist:

func configureCell (tableView: UITableView, cell: PlaylistCell, indexPath: IndexPath){

        // Setup TagView
        cell.tagListView.removeAllTags()
        for tag in array?[indexPath.row].sponsoredBy ?? [] {
            cell.tagListView.addTag(tag.name ?? "")
        }
        cell.tagListView.delegate = self


    }

Now Taglist Delegate method:

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

I am getting name of tag pressed, but need to get value of the same.


Solution

  • You can assign a tag to tagListView with indexPath.row like in cellForRow

    tagListView.tag == indexPath.row
    

    Now Taglist Delegate method:

    func tagPressed(_ title: String, tagView: TagView, sender: TagListView) {
        let yourObject = yourArrayOfTagListContainer[tagView.tag]
    
        print("Tag pressed: \(yourObject.value)")
    }
    

    Hope it helps