2017-02-26 64 views
0

嗨我試圖實現聊天視圖。UITableViewCell的高度不起作用

我的問題是堆棧視圖的自動佈局是不工作...

更新時間:

我解決了..

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

    } 

正如你可以看到紅色UILable和我設置的錨在細胞內,但它無法正常工作。

你能幫我嗎?

感謝

+0

堆棧視圖可能是矯枉過正在這個簡單的情況。 – matt

+0

感謝您的建議 –

回答

1

只需設置你的細胞的UILabel的numberOfLines爲0,然後它會自動返回高度標籤和它管理表格單元格高度

+0

謝謝它可以幫助我很多......但不影響性能?我認爲在滾動時計算uilabel會影響滾動性能。您怎麼看待它? –