2017-03-09 70 views
1

我一直有對齊問題。消息標籤一直在寫時間戳和用戶名正在消失。我已經嘗試了建議的約束條件,但沒有任何工作適合我。TableView Cell中的對齊

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


     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 

     //Transform Data From^to load at the bottom 
     tableView.transform = CGAffineTransform (scaleX: 1,y: -1); 
     cell?.contentView.transform = CGAffineTransform (scaleX: 1,y: -1); 
     cell?.accessoryView?.transform = CGAffineTransform (scaleX: 1,y: -1); 

     //Set username label to display username 
     let usernameLabel = cell?.viewWithTag(1) as! UILabel 
     usernameLabel.text = generalRoomDataArr[indexPath.row].username 


     //Set message label to display message 
     let messageLabel = cell?.viewWithTag(2) as! UILabel 
     messageLabel.text = generalRoomDataArr[indexPath.row].message 
     messageLabel.numberOfLines = 0 

     //initialize UI Profile Image 
     let imageView = cell?.viewWithTag(3) as! UIImageView 

     //Make Porfile Image Cirlce 
     imageView.layer.cornerRadius = imageView.frame.size.width/2 
     imageView.clipsToBounds = true 

     //Set timeStampLabel to current time AGO 
     let timeStampLabel = cell?.viewWithTag(4) as! UILabel 
     timeStampLabel.text = generalRoomDataArr[indexPath.row].timeStamp 
     timeStampLabel.numberOfLines = 0 

     //Loading and change of Usesrs profile image on chat cell 
     let userProfileChatImage = generalRoomDataArr[indexPath.row].photoURL 

     //Load profile image(on cell) with URL & Alamofire Library 
     let downloadURL = NSURL(string: userProfileChatImage!) 
     imageView.af_setImage(withURL: downloadURL as! URL) 

     // your cell coding 
     return cell! 
    } 

Problem

StoryBoard

//TableView Cell word wrap (Dynamic Text) 
     self.tableView.dataSource = self 
     self.tableView.delegate = self 
     self.tableView.estimatedRowHeight = 78 
     self.tableView.rowHeight = UITableViewAutomaticDimension 

我是否需要時間戳標籤設置爲0呢?

messageLabel.numberOfLines = 0 
+1

看起來像您的表格單元格未調整其高度以適應具有更多文本的標籤。在提供的圖片中,它們都與我的尺寸相同。 爲了真正瞭解問題所在,請發佈剩餘的tableview代碼以及您設置的約束條件。 – BJHStudios

+0

閱讀此可能會有所幫助。 http://www.appcoda.com/self-sizing-cells/ –

+0

我讀過那篇文章,一切都很好,直到我添加了時間戳。我需要什麼限制才能避免messageLabel重疊和空間? – codechicksrock

回答

0

我以前使用的標籤就像你正在使用的那樣,發現了約NSMutableAttributedString()。有大量的視頻和網頁解釋它是如何工作的。如果有人喜歡特定的人,他們可以在評論中鏈接它!我使用attributedStringUITextView爲了顯示我需要的一切(基於文本)。我在YouTube上推遲this視頻(從12:00開始),因爲它很難在文字中解釋。爲了解決創建適合不同數量文本的UITableViewCell的問題,您需要拖入不具有特定寬度或高度約束的UITextView。這將允許它隨着單元改變。然後,用你的代碼...

self.tableView.estimatedRowHeight = 78 
self.tableView.rowHeight = UITableViewAutomaticDimension 

你的細胞會動態變化。一個竅門得到這個工作是做下列步驟:

1)使用isUserInteractionEnabled = false 2)取消選中EditableSelectable在界面生成器(故事板UITextView禁用用戶交互),以及在IB取消選中Scrolling Enabled

0

如果您已捕獲該屏幕截圖中的所有約束。我想你錯過了時間戳標籤和消息標籤之間的垂直間距約束。添加此約束後,您還需要更改三個標籤中的一個標籤的垂直壓縮阻力和擁抱優先級。我建議減少消息標籤的。

+0

謝謝大家誰貢獻! :) – codechicksrock