单选按钮代码在 swift 的表格视图单元格中不起作用

问题描述 投票:0回答:0

我的 regularBtn,regularBtn 在故事板中像这样在 tableview 单元格的视图中buttons design hierarchy in tableview

和按钮代码:但在这里我无法进入

if sender == cell?.regularBtn{
并打印。但只能打印 sender.tag... 如何进入
cell?.regularBtn and sender == cell?.regularBtn
以更改其图像并获得价值
radioVal
。我哪里错了。请指导我

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "EventslistPopupCell", for: indexPath) as! EventslistPopupCell
cell.titleLbl.text = popupData[indexPath.row].title//titleArray[indexPath.row]

cell.regularBtn.tag = indexPath.row
cell.vipBtn.tag = indexPath.row

cell.regularBtn.addTarget(self, action: #selector(radioButnTapped(sender:)), for: .touchUpInside)
cell.vipBtn.addTarget(self, action: #selector(radioButnTapped(sender:)), for: .touchUpInside)

return cell
}


var radioVal = ""
@objc func radioButnTapped(sender: UIButton) {
print("print \(sender.tag)")

guard let indexPath = self.tableView.indexPathForRow(at: sender.convert(sender.frame.origin, to: self.tableView)) else {
    return}
let cell = self.tableView.cellForRow(at: indexPath) as? EventslistPopupCell

if !sender.isSelected{
    if sender == cell?.regularBtn{
        print("inside regular")
        cell?.regularImg.image = #imageLiteral(resourceName: "radio1")
        cell?.vipImg.image = #imageLiteral(resourceName: "radio2")
        
        self.radioVal = "R"
    }else if sender == cell?.vipBtn{
        print("inside VIP")
        
        cell?.regularImg.image = #imageLiteral(resourceName: "radio2")
        cell?.vipImg.image = #imageLiteral(resourceName: "radio1")
        
        self.radioVal = "V"
    }
}
}

请指导我解决问题

swift uitableview button action
© www.soinside.com 2019 - 2024. All rights reserved.