2017-08-27 24 views
0

我正在與控制器簡單的聯繫人應用程序有兩種:ContactTableViewController和custome細胞:ContactTableViewCell。我創建了這裏的款式是定製自定義表視圖細胞,identifierContactTableViewCell,而且類也是ContactTableViewCell。 小區有兩個UILabel領域,其中暴露於ContactTableViewCell.swift類,如下所示:自定義的UITableViewCell只顯示一個的UILabel,即使在故事板

import UIKit 

class ContactTableViewCell: UITableViewCell { 

    @IBOutlet weak var name: UILabel! 
    @IBOutlet weak var info: UILabel! 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     print("cell loaded with name: ", name) 
     // Initialization code 
    } 

    override func setSelected(_ selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

} 

現在,我想在我的ContactViewController來顯示他們,我的控制器相關的部分看起來像這樣:

// MARK: - Table view data source  
    override func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 2 
    } 


    // here we communicate with parts of the app that owns the data 
    override func tableView(_ tableView   : UITableView 
          , cellForRowAt indexPath: IndexPath 
          ) -> UITableViewCell { 

     let id = "ContactTableViewCell" 

     // deque a cell as an instance of ContactTableViewCell 
     guard let cell = tableView.dequeueReusableCell(withIdentifier: id, for : indexPath) 
     as? ContactTableViewCell else { 

      fatalError("dequed cell is not instance of ContactTableViewCell") 
     } 

     cell.name.text = "hello" 
     cell.info.text = "hello information" 

然而,當我運行仿真我只看到「你好」兩行,即使它應該是這樣的:

hello 
hello information 

重複兩次。這裏有什麼問題?

+1

您是否將自定義單元格配置爲有足夠空間顯示兩個標籤? –

+1

難道是'cell.info'被隱藏了嗎?你有沒有設置佈局約束?細胞的高度是多少? – Larme

+0

@MuhammadHassan我將「行高」設置爲90,但它仍然顯示在我的手機上,非常窄,像35。這絕對是問題所在。儘管我的故事板看起來很寬。 – chibro2

回答

1

很可能您沒有足夠的空間來放置兩個標籤。您可以通過設置行的高度在故事板:

  1. 選擇你的tableview
  2. 轉到尺寸檢查
  3. 更改Row Height

Change row height

OR

你可以實施tableView(_:heightForRowAt:)方法i你的tableView的代表。即

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    // return desired height for your cell 
    return 90.0 
} 
1

這是一個contraint問題。您必須爲名稱UILabel和信息UILabel定義高度,寬度,x和y位置。字體大小的地方有UILabels和寬度高度的主導作用,可基於的UILabel X字體大小的字符數,所以你不必明確地表達對一個UILabel高度和寬度的限制,但對於一個UIButton你必須明確定義x,y,width,height的約束。對於UILabel,我們只需要定義x和y約束。

,當你不明確定義你的UI元素的約束會發生什麼事是你查看的渲染將有不可預知的結果和UI元素往往只是不會出現在視圖。

它看起來像,從您的代碼,您使用的XCode設計師加入您的UILabels所以這是你可以用什麼來添加約束。您也可以編程方式添加約束。但我確定你正在使用XCode Storyboard設計器。

無論是通過編程方式還是通過XCode設計器,您都需要爲名稱UILabel添加一個約束到超級視圖的頂部和左側,稍後可以重新定位,然後將信息UILabel的x和y對齊限制爲名稱UILabel,水平對齊到名稱UILabel和+8垂直間距到名稱UILabel的底部,這會將信息UILabel放置在名稱UILabel的下方並居中。

請參見本指南:https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithConstraintsinInterfaceBuidler.html

這#1回答:https://stackoverflow.com/a/34448801/1258525

不是前兩個限制,我已經在這張照片盤旋,這定義了視頻質量的UILabel x和y: enter image description here

然後看下面的下一個標籤如何,開始閾值限制自己到視頻質量標籤的前沿,然後是VideoQuality標籤下面的分隔線:

enter image description here

+0

嘿@Brian Ogden你給了一個很棒的答案!但我曾與穆罕默德達成一致,接受他的回答!但是這個也非常好! – chibro2

1

我的理論的第二標籤被截斷不在第二標籤獲得足夠的寬度,並且可以是線的數量是1。因此嘗試設置線的數目爲0,並檢查寬度和自動佈局限制。

相關問題