自定义的UITableViewCell只显示一个的UILabel,即使在故事板

问题描述:

我正在与控制器简单的联系人应用程序有两种: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. 选择你的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 
} 

这是一个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。因此尝试设置线的数目为0,并检查宽度和自动布局限制。