ios - 展开 UITableViewCell 以在选择时显示附加文本标签

标签 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
    }
}

最佳答案

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

关于ios - 展开 UITableViewCell 以在选择时显示附加文本标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30962157/

相关文章:

iOS : detect the movie aspect ratio

swift - 如何让物体不停地朝一个方向移动

ios - 核心数据,在实体上设置关系会导致获取结果 Controller 委托(delegate)回调出错

iphone - UITableView注册Nib :forCellReuseIdentifier:

iphone - 显示自定义 UIButton 上的灰色叠加层

ios - TableView 在 Storyboard 中卡住了第一响应者并退出

ios - 将 Navigation 与 Controller 结合到 PageViewController

iphone - 在具有多个部分的 UITableView 中进行 Segue,每个部分包含从单个数组中过滤的对象

java - 获取具有某些条件的 tableView 项目的特定行(JavaFX)

Android 的 addJavascriptInterface 在 iOS 中的等价物