自定义 UITableViewCell 中的视图约束在单元格重用时重置

Constraints of views in custom UITableViewCell getting reset on cell reuse

我 运行 遇到了在自定义 table 单元格中显示 ImageView 的问题。我正在尝试执行以下操作:在 cellForRowAtIndexPath 中,我检查用户 ID 是否与保留对象内的 ID 匹配。如果是这样,ImageView(默认情况下隐藏)将在单元格的右侧可见。这一切都很好,直到 iOS 开始重新使用单元格。在被重用的单元格上,ImageView 总是在单元格的左侧结束,而不是右侧(它的正常位置)。我试图找出发生这种情况的原因,但我运气不佳。我将不胜感激。

编辑:看来我和这个用户遇到了同样的问题: Custom cell imageView shifts to left side when editing tableView

EDIT2:经过更多的调试,我发现每当单元格被重用时,它的约束就会改变。首次创建单元格时调用 constraintsAffectingLayoutForAxis(LayoutConstraintsAxis.Horizontal)

constraintsAffectingLayoutForAxis(LayoutConstraintsAxis.Vertical)
显示两个空数组。这些单元格是通过 Storyboard 制作的,所以我猜我无法使用这些方法读取它们(?)
在细胞被重用并且我再次调用上面的方法后,它突然有了一堆约束。然而,这些约束都是错误的,我认为这是图像视图放错地方的原因。

这是创建单元格后的日志:

Timelabel holds Optional(2) constraints
Horizontal constraints are: Optional([])
Vertical constraints are: Optional([])
ImageView holds Optional(4) constraints
Horizontal constraints are: Optional([])
Vertical constraints are: Optional([])  

下面是我将单元格滚动出视图并返回视图后打印的内容,导致它被重新使用:

Timelabel holds Optional(2) constraints
Horizontal constraints are: Optional([<NSLayoutConstraint:0x15d02a60      H:|-(38)-[UILabel:0x15f68500]   (Names: '|':UITableViewCellContentView:0x15f7de60 )>, <NSContentSizeLayoutConstraint:0x15f4e3f0 H:[UILabel:0x15f68500(103)] Hug:251 CompressionResistance:750>])
Vertical constraints are: Optional([<NSLayoutConstraint:0x15f69600 V:|-(2)-[UILabel:0x15f68500]   (Names: '|':UITableViewCellContentView:0x15f7de60 )>, <NSContentSizeLayoutConstraint:0x15f76d10 V:[UILabel:0x15f68500(21)] Hug:251 CompressionResistance:750>])
ImageView holds Optional(4) constraints
Horizontal constraints are: Optional([<NSLayoutConstraint:0x15d18390 H:[UIImageView:0x15f64260(47)]>, <NSLayoutConstraint:0x15f7c900 H:[UIImageView:0x15f64260]-(29)-|   (Names: '|':UITableViewCellContentView:0x15f7de60 )>, <NSAutoresizingMaskLayoutConstraint:0x15f77b90 h=--& v=--& H:[UITableViewCellContentView:0x15f7de60(280)]>])
Vertical constraints are: Optional([<NSLayoutConstraint:0x15db4570 V:|-(0)-[UIImageView:0x15f64260]   (Names: '|':UITableViewCellContentView:0x15f7de60 )>, <NSLayoutConstraint:0x15f736e0 V:[UIImageView:0x15f64260(48)]>])

我的 cellForRowAtIndexPath 方法:

func tableView(tableview: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cellIdentifier = "ReservationCell"

    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? ReservationCell
    let res: Reservation = reservationArray[indexPath.row]
    let startTime = DateUtils.parseUTCDateTimeStringToTimeString(res.start)
    let endTime = DateUtils.parseUTCDateTimeStringToTimeString(res.end)
    cell!.delegate = self
    let storedUserID = ELabAccount.getUserID()
    let reservationUserID = String(res.reservedForId)
    //Check if reservation belongs to currently logged in user
    if(storedUserID == reservationUserID){
        let buttons = NSMutableArray()
        buttons.sw_addUtilityButtonWithColor(UIColor.lightGrayColor(), title: LocalizedString("edit"))
        buttons.sw_addUtilityButtonWithColor(UIColor.redColor(), title: LocalizedString("delete"))
        cell!.rightUtilityButtons = buttons as [AnyObject]
        cell!.imageView!.hidden = false
        cell!.backgroundColor = UIColor(red:0.42, green:0.64, blue:0.76, alpha:1.0)
    }else{
        cell!.backgroundColor = UIColor(red:1.00, green:0.55, blue:0.25, alpha:1.0)
    }
    cell!.timeLabel.text = startTime + " - " + endTime
    cell!.nameLabel.text = res.reservedForName

    return cell!
}

always ends up on the left side 不是很清楚,但我猜你遇到了一个正在发生的问题:当你开始重复使用单元格时,图像出现在理论上不应该出现的地方。

因为id匹配时显示imageview,不匹配时不隐藏imageview。因此,通过重复使用先前ID匹配成功的单元格,将显示图像。

如果匹配失败则隐藏图像视图:

if(storedUserID == reservationUserID){
       ....
} else{
    // hides the image view
    cell!.imageView!.hidden = true
    cell!.backgroundColor = UIColor(red:1.00, green:0.55, blue:0.25, alpha:1.0)
}

好吧,我设法找到了解决方法。我最终做的是在我的自定义 table 单元格的内容视图中将 translateAutoresizingMaskIntoConstraints 标志设置为 false。这导致 imageView 在单元格重用后不再四处移动,但它确实将每个子视图向下移动,以便它们只有一半可见。使用情节提要添加一些额外的约束解决了这个问题。