自动布局问题 - 垂直间距

问题描述 投票:0回答:2

在带有Swift 3的Xcode 8中,我使用自定义单元格进行自动布局。从JSON插入单元格的信息,所以我的代码如下:

    //the method returning each cell of the list
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

    let cell = self.tableView.dequeueReusableCell(withIdentifier: "Feedcell", for: indexPath) as! FruitTableViewCell

    //getting the hero for the specified position
    let hero: List_Feed
    hero = heroes[indexPath.row]

    cell.labelAnswer.text = hero.answer
    cell.labelQuestion.lineBreakMode = .byWordWrapping
    cell.labelQuestion.text = hero.question
    cell.labelQuestion.sizeToFit()
    cell.labelAnswer.sizeToFit()

    return cell
    }

enter image description here

enter image description here

问题是我为每个单元格获得了相同的标签问题高度,但文本大小和行大小不同。怎么解决这个? :C

enter image description here

ios swift autolayout interface-builder
2个回答
0
投票

当Cell重新使用标签时您必须在用于下一个单元格之前清空标签

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

    let cell = self.tableView.dequeueReusableCell(withIdentifier: "Feedcell", for: indexPath) as! FruitTableViewCell

    //EMPTY LABELS
    cell.labelAnswer.text = ""
    cell.labelQuestion.text = ""

     self.layoutIfNeeded()

    //getting the hero for the specified position
    let hero: List_Feed
    hero = heroes[indexPath.row]

    cell.labelAnswer.text = hero.answer
    cell.labelQuestion.lineBreakMode = .byWordWrapping
    cell.labelQuestion.text = hero.question
    cell.labelQuestion.sizeToFit()
    cell.labelAnswer.sizeToFit()

    return cell
}

希望它对你有所帮助


0
投票

1-设置垂直内容拥抱优先权今天标签为1000

2-将标签底部[在今天的标签下]钩到图像的顶部

3-最下面的标签底部到contentView的底部

编辑:

在cellForRowAt中返回单元格之前添加这两行

  cell.layoutSubviews()

  cell.layoutIfNeeded()
© www.soinside.com 2019 - 2024. All rights reserved.