无法计算约束的精确值

问题描述 投票:0回答:1

我正在计算堆栈视图的尾随约束,这是在表视图中。似乎计算没有给出确切的价值。请参阅下面的截图。我的计算/逻辑不正确吗?如何解决这个问题,以便我能获得准确的价值?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    guard let cell = tableView.dequeueReusableCell(withIdentifier: "tableCellID", for: indexPath) as? TableViewCell else {
        fatalError("Can't find cell")
    }

    let profile = array[indexPath.row]

    if profile.isDisabled1 || profile.isDisabled2 || profile.isDisabled3 {

        let totalHiddenViews: Int = (profile.isDisabled1 ? 1 : 0) + (profile.isDisabled2 ? 1 : 0) + (profile.isDisabled3 ? 1 : 0)

        let singleViewWidth = (UIScreen.main.bounds.size.width - 32)/3 // 32 is sum of leading and trailing constraints

        cell.stackViewTrailing.constant = CGFloat(totalHiddenViews) * (singleViewWidth + 8)

    } else {
        cell.stackViewTrailing.constant = 8
    }

    cell.view1.isHidden = profile.isDisabled1
    cell.view2.isHidden = profile.isDisabled2
    cell.view3.isHidden = profile.isDisabled3

    return cell
}

Scraanshot

ios swift uitableview uistackview
1个回答
1
投票

你也有2-cell layout的问题。我的直觉说你的一个神奇数字是不正确的,我在这里看到的直接取决于可见细胞数量的是这一行。

cell.stackViewTrailing.constant = CGFloat(totalHiddenViews) * (singleViewWidth + 8)

我将从检查该常量的完整性开始,然后从那里开始使用依赖于可见单元数量的代码。

© www.soinside.com 2019 - 2024. All rights reserved.