Shawn Baek
Shawn Baek

Reputation: 1967

UITableViewCell's height is not working

Hi I'm trying to implement chat view.

My problem is Autolayout of the stack view is not working...

Updated:

I solved..

//Here is my UITableViewCell.

enum ChatType: Int {
    case Message
    case Photo
}

class BubbleCell: UITableViewCell {

    //Message Bubble
    var bubbleLbl = UILabel()
    var eventLbl = UILabel()

    //Photo Bubble
    var paymentView = UIStackView()
    var itemImg = UIImageView()

    //Photo Bubble
    var stackView = UIStackView()

    //settings
    var owner = Bool()
    var type = String()
    var event = Bool()

    //stackMargin
    var topInset: CGFloat = 5.0
    var bottomInset: CGFloat = 5.0
    var leftInset: CGFloat = 0.0
    var rightInset: CGFloat = 0.0

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

        super.init(style: style, reuseIdentifier: reuseIdentifier)


    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func configureCell(owner:Bool, type:ChatType, isLast:Bool) {

        self.owner = owner

        //set Autolayout
//        contentView.translatesAutoresizingMaskIntoConstraints = false
        stackView.translatesAutoresizingMaskIntoConstraints = false
        bubbleLbl.translatesAutoresizingMaskIntoConstraints = false
        eventLbl.translatesAutoresizingMaskIntoConstraints = false
        paymentView.translatesAutoresizingMaskIntoConstraints = false
        itemImg.translatesAutoresizingMaskIntoConstraints = false

        //set stackView
        stackView.axis = .vertical
        stackView.spacing = 4
        stackView.distribution = .fill


        bubbleLbl.backgroundColor = .red



        addSubview(stackView)

        if owner == true {

            leftInset = 90.0
            rightInset = 16.0
            stackView.alignment = .trailing

        }else {

            leftInset = 16.0
            rightInset = 90.0
            stackView.alignment = .leading

        }



        stackView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 20.0).isActive = true
        stackView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -20.0).isActive = true
        stackView.topAnchor.constraint(equalTo: self.topAnchor, constant: 5.0).isActive = true
        stackView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -5.0).isActive = true


        switch type {
            case .Message :

                print("Message")
                stackView.addSubview(bubbleLbl)

            case .Photo:

                print("Hellow")


        }


        // add updated time to lastCell
        if isLast {

            eventLbl.translatesAutoresizingMaskIntoConstraints = false
            stackView.addSubview(eventLbl)
        }

        stackView.layoutSubviews()
        stackView.layoutIfNeeded()

        layoutIfNeeded()
        layoutSubviews()



    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code



    }


}

enter image description here

And Here is my TableVieController

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


        //define cell
        let bubble = tableView.dequeueReusableCell(withIdentifier: "BubbleCell", for: indexPath) as! BubbleCell

        let object = results[indexPath.row]

        var checkLastCell = false

        if indexPath.row == (results.count - 1) {

            checkLastCell = true


        }

        bubble.configureCell(owner: object.isMyBubble!, type: .Message, isLast: checkLastCell)


        bubble.bubbleLbl.text = object.messageTxt
        bubble.eventLbl.text = object.msgDate


        bubble.sizeToFit()

        return bubble

    }

As you can see red is UILable and I've set anchor inside the cell but It is not working.

Could you helping me?

Thanks

Upvotes: 0

Views: 84

Answers (1)

Pratik Patel
Pratik Patel

Reputation: 1421

Just set your cell's UILabel's numberOfLines as 0 and then it will returns auto height for label and the it manages table cell height

Upvotes: 1

Related Questions