简体   繁体   English

点击Swift后修改tableview中的单元格内容

[英]Modify cell contents in tableview after clicking in Swift

I have a chat app with 2 custom cells, sender and receiver cell.我有一个带有 2 个自定义单元格、发送方和接收方单元格的聊天应用程序。 Once the cells are populated, only the ones with "show image" text can be clicked.填充单元格后,只能单击带有“显示图像”文本的单元格。 I am trying to click on these cells so that我正在尝试单击这些单元格,以便

  1. i can hide the label我可以隐藏 label
  2. I can show the image from the url我可以显示来自 url 的图像

The tap function works but i am not able to do the above steps.水龙头 function 工作,但我无法执行上述步骤。 Kindly help because i do not know how to go about this请帮忙,因为我不知道如何 go 关于这个

Here is my code这是我的代码

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

Issues in your tap function code.您的水龙头 function 代码中的问题。 When you want to get a cell from the index path use the cellForRow method of table view.当您想从索引路径中获取单元格时,请使用表格视图的 cellForRow 方法。 so instead of this所以而不是这个

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

use this for both tap action.将其用于两个点击操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM