Box House
Box House

Reputation: 177

Swift 4: When a user clicks on a UiButton how do I grab the string that is selected in a UICollectionView?

Question: I am trying to know which town someone selected when they clicked on a UIButton. When the user clicks on the UIButton I want to be able to know the "name" of which townName the user selected. How can I do this? I was able to pass the indexPath.row successfully but I am not sure how to pass the name of the town the user selected. This is my code below. Would love any help on this!

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! TownCell
    let name = townName[indexPath.row].name
    let regionId = townName[indexPath.row].region_code

    cell.townLabel.text = "\(name), \(regionId)"

    cell.button.tag = indexPath.row
    print(townName[indexPath.row].name)
    print(townName[indexPath.row].region_code)
    return cell
}


    lazy var button: UIButton = {
        let button = UIButton(type: .system)
        button.backgroundColor = greenCTAColor
        button.layer.cornerRadius = 15
        button.setTitle("select", for: .normal)
        button.setTitle("selected", for: .selected)
        button.setTitleColor(.white, for: .normal)
        button.setTitleColor(.white, for: .selected)
        button.tintColor = .clear
        button.titleLabel?.font = UIFont(name: "ProximaNova-Semibold", size: 16)
        button.addTarget(self, action: #selector(selectedAction), for: .touchUpInside)
        return button
    }()

    @objc func selectedAction(sender: AnyObject) {
        let currentCellNumber = sender.tag
        if let button = sender as? UIButton {
            if button.isSelected {
                button.isSelected = false
                button.backgroundColor = greenCTAColor
                print("User unselected cell number: \(currentCellNumber!)")
            } else {
                button.isSelected = true
                button.backgroundColor = blueCTAColor
                print("User selected cell number: \(currentCellNumber!)")
            }
        }
    }

Upvotes: 0

Views: 101

Answers (1)

Iraniya Naynesh
Iraniya Naynesh

Reputation: 1165

Just get the town from the townName array based on the button tag you set in collection view, and is your action method is calling? if its calling below will work

@objc func selectedAction(sender: UIButton) {
        let currentCellNumber = sender.tag
        if button.isSelected {
            button.isSelected = false
            button.backgroundColor = greenCTAColor
            print("User unselected cell number: \(currentCellNumber!)")
        } else {
            button.isSelected = true
            button.backgroundColor = blueCTAColor
            print("User selected cell number: \(currentCellNumber!)")
        }

        let town = townName[currentCellNumber]
        print(town.name)
    }

In cellForItemAt add action to the button for that cell

cell.button.addTarget(self, action: #selector(selectedAction), for: .touchUpInside)

I always create button and action in the collectionViewCell class and than use delegate patten to pass around data. but as you have created action in the same controller above will work, as action is set to self it will call selectedAction.

Upvotes: 2

Related Questions