单击 Swift 后修改表格视图中的单元格内容

Modify cell contents in tableview after clicking in Swift

我有一个聊天应用程序,其中包含 2 个自定义单元格、发送方和接收方单元格。填充单元格后,只能单击带有“显示图像”文本的单元格。我正在尝试单击这些单元格,以便

  1. 我可以隐藏标签
  2. 我可以显示来自 url
  3. 的图像

tap 功能有效,但我无法执行上述步骤。请帮忙,因为我不知道该怎么做

这是我的代码

    @objc
func tapFunctionCell(sender:UITapGestureRecognizer) {
    print("tap again sen")

    let tapLocation = sender.location(in: tblViewChat)
    let indexPath = self.tblViewChat.indexPathForRow(at: tapLocation)
    let position = indexPath?.row ?? 0
    print(position)
    
    let cell = tblViewChat.dequeueReusableCell(withIdentifier: "senderCell") as! SenderTblCell
    
        cell.lblMessage.isHidden = true
       
        let url = URL(string:objChatVM.getMessage(index:position))
        print(url)
        cell.ImageB.kf.setImage(with:url)
        cell.ImageB.isHidden = false

}
@objc
    func tapFunction(sender:UITapGestureRecognizer) {
        print("tap again receive")
        let cell2 = tblViewChat.dequeueReusableCell(withIdentifier: "receiverCell") as! ReceiverTblCell
        
         cell2.lblMessage.isHidden = false
        let tapLocation = sender.location(in: tblViewChat)
        let indexPath = self.tblViewChat.indexPathForRow(at: tapLocation)
        let position = indexPath?.row ?? 0
        print(position)
          
                 let url = URL(string:objChatVM.getMessage(index:position))
            print(url)
                 cell2.imageButton.kf.setImage(with:url)
                 cell2.imageButton.isHidden = false
        
       
    }

点击功能代码中的问题。当您想从索引路径中获取单元格时,请使用 table 视图的 cellForRow 方法。所以而不是这个

let cell = tblViewChat.dequeueReusableCell(withIdentifier: "senderCell") as! SenderTblCell

将此用于两个点击操作。

let cell = tblViewChat.cellForRow(at: indexPath) as! SenderTblCell