Custom tableview cell – UIPlacement

Hey All,

 

I have this UITable with a custom cell. This table gets filled with data from an json file. I want to create a chat like function so the contents are messages between 2 people. To determine who sends what i use an if statement.

 

I want to Anchor messages send by the current logged in user on the left hand side and messages received on the right hand side of the viewcontroller. Also the messages need to get placed underneath eachother with some spacing.

 

I have tryed placing the mycell.content2 in a different UITextView. I have added a UIView in the hope that would get me the desired result but up till now nothing has worked.

 

This is somewhat what i would like to achieve

https://ibb.co/gjv4jtM

 

How can i achieve this. Is it even possible with an UITableView or do i really need to switch to an UICollectionView?

 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var mycell = self.tableChat.dequeueReusableCell(withIdentifier: mycellID) as! ChatCell
let userId: String? = KeychainWrapper.standard.string(forKey: "id")

//if im the one receiving
if mydata[indexPath.row].from != userId {
mycell.chatContent.backgroundColor = orangeBanner
mycell.chatContent.textAlignment = .right
mycell.chatContent.textContainerInset = UIEdgeInsets(top: 10, left: 25, bottom: 10, right: 5)
mycell.chatContent.sizeToFit()
mycell.content2 = mydata[indexPath.row].from
}
//if im the on sending
if mydata[indexPath.row].from == userId{
mycell.chatContent.backgroundColor = grayBanner
mycell.chatContent.textAlignment = .left
mycell.chatContent.sizeToFit()
mycell.chatContent.textContainerInset = UIEdgeInsets(top: 10, left: 5, bottom: 10, right: 25)
mycell.content = mydata[indexPath.row].from
}

return mycell
}

 

Custom Cell

 

import Foundation
import UIKit

class ChatCell: UITableViewCell {

    var content: String?
    var content2: String?


    var chatContent : UITextView = {
        var textView = UITextView()

        textView.translatesAutoresizingMaskIntoConstraints  = false
        textView.font = UIFont.boldSystemFont(ofSize: 12)
        textView.heightAnchor.constraint(equalToConstant: 60).isActive = false
        textView.isScrollEnabled = false
        textView.isUserInteractionEnabled = false
        return textView
    }()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?){
        super.init(style: style, reuseIdentifier: reuseIdentifier)

    }
  required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    override func layoutSubviews() {
        super.layoutSubviews()
      
    
        if  let content = content {
            self.addSubview(chatContent)

            chatContent.text = content
            chatContent.leftAnchor.constraint(equalTo: self.lefttAnchor, constant: 10).isActive = true
            chatContent.translatesAutoresizingMaskIntoConstraints = false
    
            chatContent.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
            chatContent.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        }
      

 
        if  let content2 = content2 {
            self.addSubview(chatContent)

            chatContent.text = content2
            chatContent.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -10).isActive = true
            chatContent.translatesAutoresizingMaskIntoConstraints = false
    
            chatContent.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
            chatContent.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        }

    }
}

Powered by WPeMatico

About

You may also like...

Comments are closed.