自动布局问题 - 垂直间距
Issue with auto layout - vertical spacing
在 Xcode 8 和 Swift 3 中,我正在使用具有自动布局的自定义单元格。从 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
}
问题是每个单元格的 labelQuestion 高度都相等,但文本大小和行大小不同。如何解决这个问题? :c
1- 将今天标签的垂直内容拥抱优先级设置为 1000
2- 将标签底部 [ 即今天标签下 ] 钩到 imageView 的顶部
3- 将最下方标签的底部挂接到 contentView 的底部
编辑:
在 cellForRowAt
中的 return 单元格前添加这两行
cell.layoutSubviews()
cell.layoutIfNeeded()
由于 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
}
希望对您有所帮助
在 Xcode 8 和 Swift 3 中,我正在使用具有自动布局的自定义单元格。从 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
}
问题是每个单元格的 labelQuestion 高度都相等,但文本大小和行大小不同。如何解决这个问题? :c
1- 将今天标签的垂直内容拥抱优先级设置为 1000
2- 将标签底部 [ 即今天标签下 ] 钩到 imageView 的顶部
3- 将最下方标签的底部挂接到 contentView 的底部
编辑:
在 cellForRowAt
中的 return 单元格前添加这两行 cell.layoutSubviews()
cell.layoutIfNeeded()
由于 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
}
希望对您有所帮助