正如问题所述,我试图在用户选择 UITableViewCell 时展开它,以便在该单元格中显示其他信息。我在这里查看了类似的相关问题,并且有点让它起作用了。该应用程序没有崩溃,单元格确实展开了,但我的控制台中不断出现约束布局错误。我几乎肯定会收到这些错误,因为当未选择 UITableViewCell 时,它的高度不足以包含其他标签,即使在选择单元格之前标签是隐藏的。如果有人有建议,或者我可以通过其他方式完成该功能,我将非常感谢您的帮助。谢谢!
我正在尝试展开的自定义单元格
class ListTableViewCell: UITableViewCell {
@IBOutlet weak var wordLabel: UILabel!
@IBOutlet weak var definitionLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
self.autoresizingMask = UIViewAutoresizing.FlexibleHeight
self.clipsToBounds = true
self.definitionLabel.hidden = true
}
}
选择时展开 UITableViewCell 的 UITableView 代码
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! ListTableViewCell
if cell.definitionLabel.hidden == true {
cell.definitionLabel.hidden = false
} else {
cell.definitionLabel.hidden = true
}
currentRow = indexPath.row
tableView.beginUpdates()
tableView.endUpdates()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if currentRow == indexPath.row {
return UITableViewAutomaticDimension
} else {
return 55.0
}
}
最佳答案
在选择时,您可以调用 reloadRowsAtIndexPaths:这应该会更新此单元格的大小。 See Apple documentation
关于ios - 展开 UITableViewCell 以在选择时显示附加文本标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30962157/