UITableViewCell的高度不起作用

时间:2017-02-26 02:45:26

标签: ios swift uitableview uicollectionviewcell

您好我正在尝试实施聊天视图。

我的问题是堆栈视图的Autolayout无效......

更新:

我解决了..

//这是我的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

这是我的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

    }

正如你所看到的那样,红色是可以使用的,而且我已经在单元格中设置了锚点,但是它无效。

你能帮助我吗?

由于

1 个答案:

答案 0 :(得分:1)

只需将您单元格的UILabel的numberOfLines设置为0,然后它将返回标签的自动高度,并管理表格单元格高度