ios - 更改 Storyboard约束关系

标签 ios swift uitableview

我在更改 Storyboard 中的约束关系时遇到问题。这就是 TableView 目前的样子。 enter image description here

当单击更多按钮时,我希望表格 View 看起来像这样。 enter image description here

好的,所以我将底部约束从 imageView 拖到我的 tableViewCell.swift 文件中,并将其命名为 imageBottomConstraint。我试过 imageBottomConstraint.relation = .greaterThan 但给我一个错误,说它只是一个 get,所以关系是 equalTo,我正在尝试将它更改为 greaterThan。

这是我感到困惑和卡住的地方,所以我试图创建一个新的约束,但并不真正理解 NSLayoutConstraint(item:, attribute:, relatedBy:, toItem:, attribute:, multiplier :, 常量:)。不明白 item, attribute, relatedBy, toItem & attribute,请大神指点一下。

我认为我的灾难代码可以工作,但没有崩溃。

@IBAction func showMoreBtn() {
    let item = imageBottomConstraint.firstItem
    let attribute1 = imageBottomConstraint.firstAttribute
    let relatedBy = NSLayoutRelation.greaterThanOrEqual
    let item2 = imageBottomConstraint.secondItem
    let attribute2 = imageBottomConstraint.secondAttribute
    let multiplier = imageBottomConstraint.multiplier
    let constant = 12 

    let newConstraint = NSLayoutConstraint(item: item, attribute: attribute1, relatedBy: relatedBy, toItem: toItem, attribute: attribute2, multiplier: multiplier, constant: 12)
}

如您所见,在创建约束程序时,我不知道自己在做什么。非常感谢您的帮助。

最佳答案

您可以在 TableView 的 heightForRowAt Cell Delegate 中管理预期行为

/// Use this function to Set Height of TableCells
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
        {
            /// Is index Path More Button is cliked ?
            if indexPath.row == selectedIndex
            {
                /// If yes
                return UITableViewAutomaticDimension
            }
            else
            {
                /// if No
                /// Provide a Static Height That will Set your requiremment
                return 160
            }
        }

selectedIndex 它是单击更多按钮的索引,我的要求是一次展开一个单元格,您可以使用数组来管理需要展开的单元格

关于ios - 更改 Storyboard约束关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49912313/

相关文章:

android - 在flutter中设置childAspectRatio的正确方法

ios - 对于 UITableViewCell 内的嵌套 UITableView didSelectRowAt indexPath : is not being called

iphone - 重新加载RowsAtIndexPaths :withRowAnimation: crashes my app

ios - 是否可以为 UITableView 中的每个部分设置不同的分隔符颜色?

ios - 没有泛型约束的协议(protocol)中的泛型数组

objective-c - 无法导入 AssetsLibrary.h

ios - 不显示来自解析的数据

ObservedObject 更改时 SwiftUI View 未更新

ios - 我正在尝试创建层次结构 View 。单击每个人后, View 应展开和收缩

ios - 如何构建 TableView 以加载 View 的一半?