2017-06-18 42 views
0

我已經有了一個UITableView,並且這個單元通過對遠程服務的post調用來填充。 我已經將UILabel lineBreakMode屬性設置爲NSLineBreakByTruncatingTail,並將行設置爲2.在單元格xib上,一切似乎都很好。這裏是截圖:UILabel應該會產生省略號但不起作用

enter image description hereenter image description here

這是一段代碼我使用填充後解析從遠程服務中檢索到的JSON的文本。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     if (posts.count == 0){ 
      return tableView.dequeueReusableCell(withIdentifier: 
       TableViewCellIdentifiers.emptyCell, for: indexPath) 
     } else { 
      let cell = tableView.dequeueReusableCell(withIdentifier: 
       TableViewCellIdentifiers.postCell, for: indexPath) as! PostCell 

      let post = posts[indexPath.row] 
      cell.userNameLabel.text = post.userName 
      cell.postTextLabel.text = post.text 
      cell.timeLabel.text = post.time 
      cell.distanceLabel.text = post.distance 

      return cell 
     } 
    } 

但在這裏它的結果,也不會產生省略號,但每一件事是上覆 enter image description here

任何想法的問題,其中可能呢?

+0

缺少約束來告訴你的標籤它不能超過一定的寬度(比如時間標籤或距離標籤之前的8個點)?此外,您可能還想檢查關於Y位置或高度的黃色警告,並查看結果(修復它,並且可能爲字體大小添加適合的縮放比例) – Larme

+0

xib中的橙色線意味着有問題。你可能只是設定框架,而不是約束。 – Sulthan

回答

0

看來你有缺少約束。嘗試從標籤的右側邊緣設置一個約束到右側的TimeDistance標籤。我會讓所有標籤都有一個固定的寬度,並設置所有約束與關係Equal,UILabels與內部正確對齊文本。

0

這裏的問題是因爲你給出的寬度約束。黃色警告表明在運行時標籤寬度可以超過任何限制。此外,Y位置或高度設置不正確。您需要在運行時設置這兩個約束,否則Label將採用適合文本長度的寬度。