Ios 无法计算约束的精确值

Ios 无法计算约束的精确值,ios,swift,uitableview,uistackview,Ios,Swift,Uitableview,Uistackview,我正在计算表视图中堆栈视图的尾部约束。看来计算并没有给出确切的数值。请参阅下面的屏幕截图。我的计算/逻辑是否不正确?如何解决这个问题,使我可以得到准确的值 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "tab

我正在计算表视图中堆栈视图的尾部约束。看来计算并没有给出确切的数值。请参阅下面的屏幕截图。我的计算/逻辑是否不正确?如何解决这个问题,使我可以得到准确的值

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
}

您也有问题与。我的直觉告诉我,你的一个神奇数字是错误的,我在这里看到的,直接取决于可见细胞的数量的是这条线

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

我会先检查这个常数是否正常,然后再从代码开始,代码取决于可见单元格的数量

每个表视图单元格内的UICollectionView可能是way@RobertDresler,为什么不创建一个UICollectionView而不是UITableView?@user28434对,有多个部分我已经在使用包含这三个视图的单元视图中的堆栈视图了。UICollectionView如何比Stack view更好?我已经在我的应用程序中使用了table view,它是实时的,并且在单元格中包含许多字段。将其更改为集合视图对我来说很复杂。我们是否可以在表视图中解决此问题?8是stackview内部视图之间的间距,以及视图和SuperView之间的前导和尾随约束。