我在自定义表格单元格中显示 ImageView 时遇到问题。我正在尝试执行以下操作:在 cellForRowAtIndexPath 中,我检查用户 ID 是否与预订对象内的 ID 匹配。如果是这样,ImageView(默认情况下隐藏)将在单元格的右侧可见。在 iOS 开始重用单元格之前,这一切都工作正常。在重复使用的单元格上,ImageView 始终位于单元格的左侧,而不是右侧(其正常位置)。我试图找出为什么会发生这种情况,但运气不太好。我希望得到一些帮助。
编辑:我似乎遇到了与该用户相同的问题: Custom cell imageView shifts to left side when editing tableView
EDIT2:经过更多调试,我发现每当单元格被重用时,它的约束就会改变。首次创建单元格时调用constraintsAffectingLayoutForAxis(LayoutConstraintsAxis.Horizontal)
和
constraintAffectingLayoutForAxis(LayoutConstraintsAxis.Vertical)
显示两个空数组。这些单元格是通过 Storyboard制作的,所以我猜我无法使用这些方法读取它们(?)
在单元格被重用并且我再次调用上面的方法之后,它突然有了一堆约束。然而,这些约束都是错误的,我认为这是 ImageView 错位的原因。
这是创建单元格后的日志:
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([])
这是我将单元格滚动到 View 之外并返回 View 后打印的内容,导致它被重用:
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!
}
最佳答案
好吧,我设法找到了解决方法。我最终所做的是将自定义表格单元格的内容 View 上的translateAutoresizingMaskIntoConstraints 标志设置为 false。这导致 imageView 在单元重用后不再移动,但它确实将每个 subview 向下移动,以便它们只有一半可见。使用 Storyboard添加一些额外的约束解决了这个问题。
关于ios - 自定义 UITableViewCell 中的 View 约束在单元重用时重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34159542/