我使用 swift 5 UITableView 和自定义 Cell 它总是重叠数据

发布于 2025-01-14 03:40:37 字数 4547 浏览 5 评论 0原文

这是自定义单元格代码

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated) 

      switch getChatType!
        {
            //    MARK: Profile image chatting type = 1 (Text)
            
            
        case "1": // profile image
            textData =  UILabel()
            textData.numberOfLines = 0
            textData.font = UIFont.systemFont(ofSize: 18)
            textData.textColor = .white
            textData.backgroundColor = .clear
            textData.text = getChatData![0..<self.appDelegate.limitDisplayText]
            
            let tt : String = getChatData![0..<self.appDelegate.limitDisplayText] as String
            let constraintRect = CGSize(width: 0.66 * screenWidth,
                                        height: .greatestFiniteMagnitude)
            let boundingBox = tt.boundingRect(with: constraintRect,
                                              options: .usesLineFragmentOrigin,
                                              attributes: [.font: textData.font as Any],
                                              context: nil)
            textData.frame.size = CGSize(width: ceil(boundingBox.width),
                                         height: ceil(boundingBox.height))
            let bubbleSize = CGSize(width: textData.frame.width + 28,
                                    height: textData.frame.height + 20)
            
            profileImage = UIImageView.init()
            if(getChatData!.count > self.appDelegate.limitDisplayText)
            {
                profileImage = UIImageView.init(frame: CGRect(x: 5+self.appDelegate.chattingsTableMarginX, y: (bubbleSize.height-20)+(self.appDelegate.limitTextSizePlus/2)+getFirstOfDay!+getChatReply!, width: self.appDelegate.chattingsProfileImage, height: self.appDelegate.chattingsProfileImage))
            }else{
                profileImage = UIImageView.init(frame: CGRect(x: 5+self.appDelegate.chattingsTableMarginX, y: (bubbleSize.height-20)+getFirstOfDay!+getChatReply!, width: self.appDelegate.chattingsProfileImage, height: self.appDelegate.chattingsProfileImage))
            }
            
            if(getContactProfileImage == "")
            {
                profileImage.image = UIImage(named: "user.png")
            }else{
                profileImage.image = UIImage(contentsOfFile: "\(self.appDelegate.function.getDocumentsDirectoryString())/profiles/\(getContactProfileImage!)")
            }
            
            profileImage.clipsToBounds = true
            profileImage.layer.borderWidth = 3
            profileImage.layer.borderColor = UIColor.lightGray.cgColor
            profileImage.layer.cornerRadius = 15
            profileImage.isUserInteractionEnabled = true
            self.contentView.addSubview(profileImage)
            
      }

表格视图代码

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


    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellChattingsID) as? chattingCustomCell else { return UITableViewCell()}
    cell.getEventId = eventId
    cell.getChatData = appDelegate.chattingsDataArr[indexPath.row].chat_data
    
    cell.getChatReply = self.getChatReply(replyId: "\(appDelegate.chattingsDataArr[indexPath.row].reply)")
    
    cell.getChatReplyChatId = appDelegate.chattingsDataArr[indexPath.row].reply
    cell.getChatID = appDelegate.chattingsDataArr[indexPath.row].chat_id
    cell.getChatType = appDelegate.chattingsDataArr[indexPath.row].chat_type
    cell.getOwner = appDelegate.chattingsDataArr[indexPath.row].owner
    cell.getReceiver = appDelegate.chattingsDataArr[indexPath.row].receiver
    cell.getUploadDate = appDelegate.chattingsDataArr[indexPath.row].upload_date
    cell.getReadDate = appDelegate.chattingsDataArr[indexPath.row].read_date
    cell.getDeleteDate = appDelegate.chattingsDataArr[indexPath.row].delete_date
    
    cell.getChatTimestamp = appDelegate.chattingsDataArr[indexPath.row].chat_timestamp
    
    cell.getFirstOfDay = self.getFirstOfDay(chatId: "\(appDelegate.chattingsDataArr[indexPath.row].chat_id)")
    cell.getActionFlag =  appDelegate.chattingsDataArr[indexPath.row].action_flag
    return cell

        }

这是我这样编码的 ,用于将数据传递到自定义单元格。 自定义单元格通过传递显示图像或文本等数据来创建元素,我将创建聊天应用程序。

当我滚动单元格重叠中的屏幕内容时 我尝试一下。

  1. 准备重用并删除内容中所有不起作用的视图。
  2. 删除 func setSelected 中的所有内容视图不起作用。

如何修复它。我尝试了 1 周以上的时间来修复它。

谢谢

this is custom cell code

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated) 

      switch getChatType!
        {
            //    MARK: Profile image chatting type = 1 (Text)
            
            
        case "1": // profile image
            textData =  UILabel()
            textData.numberOfLines = 0
            textData.font = UIFont.systemFont(ofSize: 18)
            textData.textColor = .white
            textData.backgroundColor = .clear
            textData.text = getChatData![0..<self.appDelegate.limitDisplayText]
            
            let tt : String = getChatData![0..<self.appDelegate.limitDisplayText] as String
            let constraintRect = CGSize(width: 0.66 * screenWidth,
                                        height: .greatestFiniteMagnitude)
            let boundingBox = tt.boundingRect(with: constraintRect,
                                              options: .usesLineFragmentOrigin,
                                              attributes: [.font: textData.font as Any],
                                              context: nil)
            textData.frame.size = CGSize(width: ceil(boundingBox.width),
                                         height: ceil(boundingBox.height))
            let bubbleSize = CGSize(width: textData.frame.width + 28,
                                    height: textData.frame.height + 20)
            
            profileImage = UIImageView.init()
            if(getChatData!.count > self.appDelegate.limitDisplayText)
            {
                profileImage = UIImageView.init(frame: CGRect(x: 5+self.appDelegate.chattingsTableMarginX, y: (bubbleSize.height-20)+(self.appDelegate.limitTextSizePlus/2)+getFirstOfDay!+getChatReply!, width: self.appDelegate.chattingsProfileImage, height: self.appDelegate.chattingsProfileImage))
            }else{
                profileImage = UIImageView.init(frame: CGRect(x: 5+self.appDelegate.chattingsTableMarginX, y: (bubbleSize.height-20)+getFirstOfDay!+getChatReply!, width: self.appDelegate.chattingsProfileImage, height: self.appDelegate.chattingsProfileImage))
            }
            
            if(getContactProfileImage == "")
            {
                profileImage.image = UIImage(named: "user.png")
            }else{
                profileImage.image = UIImage(contentsOfFile: "\(self.appDelegate.function.getDocumentsDirectoryString())/profiles/\(getContactProfileImage!)")
            }
            
            profileImage.clipsToBounds = true
            profileImage.layer.borderWidth = 3
            profileImage.layer.borderColor = UIColor.lightGray.cgColor
            profileImage.layer.cornerRadius = 15
            profileImage.isUserInteractionEnabled = true
            self.contentView.addSubview(profileImage)
            
      }

this is tableview code

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


    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellChattingsID) as? chattingCustomCell else { return UITableViewCell()}
    cell.getEventId = eventId
    cell.getChatData = appDelegate.chattingsDataArr[indexPath.row].chat_data
    
    cell.getChatReply = self.getChatReply(replyId: "\(appDelegate.chattingsDataArr[indexPath.row].reply)")
    
    cell.getChatReplyChatId = appDelegate.chattingsDataArr[indexPath.row].reply
    cell.getChatID = appDelegate.chattingsDataArr[indexPath.row].chat_id
    cell.getChatType = appDelegate.chattingsDataArr[indexPath.row].chat_type
    cell.getOwner = appDelegate.chattingsDataArr[indexPath.row].owner
    cell.getReceiver = appDelegate.chattingsDataArr[indexPath.row].receiver
    cell.getUploadDate = appDelegate.chattingsDataArr[indexPath.row].upload_date
    cell.getReadDate = appDelegate.chattingsDataArr[indexPath.row].read_date
    cell.getDeleteDate = appDelegate.chattingsDataArr[indexPath.row].delete_date
    
    cell.getChatTimestamp = appDelegate.chattingsDataArr[indexPath.row].chat_timestamp
    
    cell.getFirstOfDay = self.getFirstOfDay(chatId: "\(appDelegate.chattingsDataArr[indexPath.row].chat_id)")
    cell.getActionFlag =  appDelegate.chattingsDataArr[indexPath.row].action_flag
    return cell

        }

I coding like this for passing data to customcell.
and custom cell create element by passing data such as display image, or text etc , I will create chat application.

when I scroll screen content in cell overlapping
I try.

  1. prepareforreuse and remove all view in content not working.
  2. remove all content view in func setSelected not working.

how to fixed it. I try more 1 weeks to fixed it.

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文