【问题标题】:Modify cell contents in tableview after clicking in Swift在Swift中单击后修改tableview中的单元格内容
【发布时间】:2020-12-10 11:39:29
【问题描述】:

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

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

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

这是我的代码

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

【问题讨论】:

    标签: swift uitableview tableview custom-cell


    【解决方案1】:

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

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

    将其用于两个点击操作。

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

    【讨论】:

    • 让 cell = self.tblViewChat.cellForRow(at: indexPath!) as ! SenderTblCell 感谢 Raja Kishan
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多