【问题标题】:Constraints get invalidated约束失效
【发布时间】:2018-02-17 23:03:36
【问题描述】:

我正在尝试使用 UILabel 和 UIImageView 创建一个表格视图单元格。

这是我的视图控制器打开时的屏幕截图。

当我将单元格滚动出视图或点击单元格时,图像视图的框架会发生变化。

这是我的限制:

这是我的数据源方法:

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

if(indexPath.section == 0) {
    let currentVolume = viewModel.volumes?[indexPath.row];

    if let volume = currentVolume {
        if (volume.isPurchased == 0) {
            let reuseIdentiifier = Constants.ReuseIdentifier.VolumeListTableViewCellReuseId;
            let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentiifier, for: indexPath) as! VolumeTableViewCell;
            cell.setTitle(title: (volume.name));
            return cell;
        }
        else {
            let reuseIdentiifier = Constants.ReuseIdentifier.LockedVolumeListTableViewCellReuseId;
            let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentiifier, for: indexPath) as! LockedVolumeTableViewCell;
            cell.setTitle(title: (volume.name));
            return cell;
        }
    }
}
return UITableViewCell.init();
}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{
    //Disables the 'selected' cell
    tableView.deselectRow(at: indexPath, animated: true);
}

我无法弄清楚出了什么问题?

如果有任何帮助,我将不胜感激。

【问题讨论】:

  • @AamirR 这没有帮助。我还需要在图像视图之前将标题标签的尾随限制为 15 个点。
  • 以我的方式使用它有什么问题?理想情况下,我想要的是: 1) trailing = Imageview.trailing + 15 2) label.trailing = imageView.leading - 15 3) imageView.width = 20
  • 您的约束看起来不错。这似乎是您的代码中的一个问题。您能否分享您构建和设置单元的代码?
  • @LGP 我已经编辑了帖子以包含我的设置代码。
  • 当锁“跳”时,打开查看调试器,看看它是否有任何警告。

标签: ios uitableview autolayout tableview


【解决方案1】:

首先选择 imageView 并添加屏幕截图中选择的约束

并添加“垂直居中约束”

然后选择标签并添加在屏幕短中选择的约束

如果有任何问题请告诉我,希望这能奏效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    相关资源
    最近更新 更多