选中时,展开UITableViewCell以显示其他文本标签

时间:2015-06-21 07:11:00

标签: ios swift uitableview autolayout

正如问题所述,我正在尝试在用户选择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
    }
}

1 个答案:

答案 0 :(得分:2)

在选择时,您可以调用reloadRowsAtIndexPaths:这应该更新此单元格的大小。 See Apple documentation