从顶部底部有额外空间的标签?

时间:2018-02-06 07:28:51

标签: ios swift xcode autolayout

我在这下面创建了一个自定义单元格。 enter image description here

问题是当我第一次发射然后它需要高度(从顶部和底部)。但是当向上和向下滚动时。然后删除额外的高度。我无法理解为什么会出现这个问题。

顶部标签的行号已修复。 第二个标签的行号为0。 enter image description here

控制器代码

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    return UITableViewAutomaticDimension
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

    return UITableViewAutomaticDimension
}

func numberOfSections(in tableView: UITableView) -> Int {

    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 15
}

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

    let cell:SampleCell = tableView.dequeueReusableCell(withIdentifier: "SampleCell") as! SampleCell
    if indexPath.row == 0 {
        cell.labelDescription.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.f Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.f Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.f Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like "

    } else {
        cell.labelDescription.text = "skdfjksj gkfjg"
    }
    tableView.sizeToFit()
    return cell
}

UIImage的约束

enter image description here

Top UILabel的约束 enter image description here

底部UILabel的约束 enter image description here

1 个答案:

答案 0 :(得分:0)

UITableViewAutomaticDimension设为heightForRowAt

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
   return UITableViewAutomaticDimension  
}

numberOfLine的{​​{1}}设置为 0 &安培;设置UILabel