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

标签 ios swift uicollectionview swift4

问题:我想知道某人在单击 UIButton 时选择了哪个城镇。当用户单击 UIButton 时,我希望能够知道用户选择的 townName 的“名称”。我怎样才能做到这一点?我能够成功传递 indexPath.row,但我不确定如何传递用户选择的城镇名称。这是我下面的代码。希望对此有任何帮助!

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!)")
            }
        }
    }

最佳答案

只是根据你在collection view中设置的button标签从townName数组中获取town,你的action方法是不是在调用?如果下面的调用有效

@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)
    }

在 cellForItemAt 中 向该单元格的按钮添加操作

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

我总是在 collectionViewCell 类中创建按钮和操作,而不是使用委托(delegate)模式来传递数据。但是由于您在上面的同一 Controller 中创建了操作,因此操作将起作用,因为操作设置为 self 它将调用 selectedAction。

关于ios - swift 4 : When a user clicks on a UiButton how do I grab the string that is selected in a UICollectionView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50259392/

相关文章:

iphone - iOS 应用程序中的全局函数

ios - 在 AWS 套接字 S3 存储桶中缓存

ios - 将 UILabel 放置在父级的中心

ios - UICollectionViewFlowLayout minimumInteritemSpacing 不起作用

android - 用Cordova显示本地保存的图片

ios - 如何更改Cocoapod依赖项的版本

ios - collectionViewContentSize() 与 contentSize

ios - 主页按钮和 ApplicationState。重新启动应用程序时调用哪个委托(delegate)方法?

ios - 如何在 PDFView 中选择连续文本以便添加注释?

ios - 如何根据内容设置 collectionview 单元格宽度?