2016-12-16 27 views
0

我正在为Chat ViewController使用tableview。 我需要大小来适应textView。 高度和宽度。但我无法使用这两个参数。带有自动大小的单元格中的文本视图

这是第一次设置约束,它看起来像this。 而这第二次设置约束,它看起来像this

这是代码MyProfile Cell。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if messages[indexPath.row].image != nil { 
     tableView.rowHeight = 200 
     if !messages[indexPath.row].owner! { 
      let cell = self.chatTableView.dequeueReusableCellWithIdentifier("chatImageCell", forIndexPath: indexPath) as! ChatImageTableViewCell 
      cell.imageV.image = messages[indexPath.row].image 
      let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tappedAtImage(_:))) 
      cell.imageV.addGestureRecognizer(tapGesture) 
      cell.imageV.userInteractionEnabled = true 
      cell.transform = CGAffineTransformMakeRotation(CGFloat(M_PI)) 
      return cell 
     } else { 
      let cell = self.chatTableView.dequeueReusableCellWithIdentifier("chatImageCellEnemy", forIndexPath: indexPath) as! ChatImageTableViewCell 
      cell.imageV.image = messages[indexPath.row].image 
      let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tappedAtImage(_:))) 
      cell.imageV.addGestureRecognizer(tapGesture) 
      cell.imageV.userInteractionEnabled = true 
      cell.transform = CGAffineTransformMakeRotation(CGFloat(M_PI)) 
      return cell 
     } 
    } else if messages[indexPath.row].owner! { 
     tableView.rowHeight = UITableViewAutomaticDimension 
     let cell = self.chatTableView.dequeueReusableCellWithIdentifier("mymessageCell", forIndexPath: indexPath) as! MyChatTableViewCell 
     cell.transform = CGAffineTransformMakeRotation(CGFloat(M_PI)) 

     cell.textView.text = messages[indexPath.row].text! 
     cell.textView.layer.cornerRadius = 8 
     cell.textView.layer.masksToBounds = true 
     cell.textView.textAlignment = .Right 
     cell.textView.textColor = .whiteColor() 
     cell.textView.sizeToFit() 
     cell.textView.layoutIfNeeded() 


     let unix = NSTimeInterval(messages[indexPath.row].unixDate!) 
     let messageDate = NSDate(timeIntervalSince1970: unix!) 
     cell.timeLabel.text = timeAgoSinceDate(messageDate, numericDates: true) 

     return cell 

    } else { 
     let cell = self.chatTableView.dequeueReusableCellWithIdentifier("messageCell", forIndexPath: indexPath) as! ChatTableViewCell 
     tableView.rowHeight = UITableViewAutomaticDimension 
     cell.transform = CGAffineTransformMakeRotation(CGFloat(M_PI)) 

     cell.textView.text = messages[indexPath.row].text! 
     cell.textView.layer.cornerRadius = 8 
     cell.textView.layer.masksToBounds = true 
     cell.textView.textColor = .blackColor() 
     cell.textView.sizeToFit() 
     cell.textView.layoutIfNeeded() 

     let unix = NSTimeInterval(messages[indexPath.row].unixDate!) 
     let messageDate = NSDate(timeIntervalSince1970: unix!) 
     cell.timeLabel.text = timeAgoSinceDate(messageDate, numericDates: true) 

     return cell 
    } 
} 

谢谢!

回答

0

Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

阅读这篇博客,神话般的答案给出那里。希望这可以帮助你,如果不让我,那么我会去你的代码工作。

+0

感谢您的回答,但我不明白我做错了什么。你能帮我解码吗? – coolfrut

+0

实际上你面对的是什么问题,你在问题中添加的图像,图像右侧我看到输出,看起来是正确的,但是你的代码需要很多修正,或者可能是你的需要,所以你添加所有东西。有关信息什么是'messages []'。 –

+0

消息是struct Message的数组,因此消息是: struct消息var text:String? var unixDate:String? var status:Int? var owner:Bool? var id:String? var image:UIImage? } – coolfrut

相关问题