'I use swift 5 UITableView with custom Cell it alway overlapping data

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



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source