首页 文章

didSelectRowAtIndexpath未触发tableview单元格Swift iOS中的按钮

提问于
浏览
1

我在tableview单元格中有一个按钮(如下图所示),当我单击按钮时,

didSelectRowAt indexpath

没有触发,有人可以建议我怎么做到这一点?

Please note: 我正在点击按钮执行一系列操作,此外我还想

didselectRowAt indexPath

被触发 .

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 print("Table view cell has been clicked")   
}

enter image description here

2 回答

  • 0

    将插座放在自定义单元类中的按钮上 . 然后在cellForItemAt中设置标签并为按钮添加选择器 . 此示例适用于colletionView,只需更改为适合tableView .

    如果您想要每个单元格中的按钮,那么您就必须这样做 . 向单个单元格添加静态按钮不会调用didSelectItemAt,因为您点击了一个没有引用可重用单元格indexPath的按钮 .

    这样我们发送button.tag他的功能,以便我们知道按钮与哪个单元格相关 .

    class MyClassViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    
        .... // Stuff
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        ....// Create Cell
    
        cell.deleteCellButton.tag = indexPath.item
        cell.deleteCellButton.addTarget(self, action: #selector(MyClassViewController.deleteCellButtonTapped(_:)), for: .touchUpInside)
        return cell
    }
    
    func deleteCellButtonTapped(_ sender: Any) {
    
         ... // Stuff
    
          print("Selector called")
    }
    }
    
  • 0

    如果要在 UITableViewCell 上添加按钮或手势,则不会调用 didselectRowAt indexPath . 您可以在剩余的UI UITableViewCell 上添加按钮,而不是在按钮上应用清除颜色 . 用户没有显示按钮,您可以执行 didselectRowAt indexPath 方法任务 . 如果你想在按钮方法代码 indexpath 给出如下 .

    func btnChildDropDown(sender:UIButton)
        {
           let cell = sender.superview?.superview as! ChildAgeTableViewCell
           let indexPath = self.tblViewAddRooms.indexPath(for: cell)
    
        }
    

相关问题