0

我有一个有两行的静态表。第一排有开关。我只想在开关打开时启用第二行。并在开关关闭时隐藏该行。

第二行有标签和文本字段。它们都有自动布局约束。

我正在硬编码第二行的高度,如下所示

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    if indexPath.section == 0 && !reminderSwitch.on {

        if indexPath.row == 1  {

            return 0.0

        }

    }

    return 44.0

}

它做我想做的事,但我收到以下错误/警告。

无法同时满足约束。以下列表中的至少一个约束可能是您不想要的。

我知道我可以直接隐藏该行,而不是创建零高度行。但它在桌子上留下了很大的空白。我不想那样做。我想创建零高度行,但我也想删除警告。

4

1 回答 1

0

对于静态表,您的方法是正确的。您可能只需要检查如何设置约束,并添加一些属性来更改其中一些约束的值。

IBOutlet NSLayoutConstraint *_cellVerticalSpaceConstraint;

_cellVerticalSpaceConstraint.constant = 0;

如果您有动态单元格,您可以根据需要创建一个带有 xib 的自定义单元格,或者以编程方式创建它。我会使用自定义单元格来处理这种情况,因为您可能想要捕获文本字段的事件并触发其他方法。有很多这样的例子:

https://medium.com/@musawiralishah/creating-custom-uitableviewcell-using-nib-xib-files-in-xcode-9bee5824e722#.ukw4zn30o

要添加/删除行,您可以使用以下代码:

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

insertIndexPaths 和 deleteIndexPaths 是 NSIndexPaths 的数组,如下所示:

NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects:
    [NSIndexPath indexPathForRow:0 inSection:0],
    nil];
于 2015-10-26T18:16:40.777 回答