1

I try to have a custom edit view and animation for the edit mode of the cells in my UITableView, so I made a custom class, added my subviews to it and animate some frame changes in the setEditing(_:animated:) function. I also override the layoutSubviews() function, where I provide also the new frame values:

class TableViewCell: UITableViewCell {

    override func layoutSubviews() {
        self.deleteButtonView.frame.origin.x = self.viewPosition - 80
        self.disclosureButtonView.frame.origin.x = self.contentView.bounds.width - (self.viewPosition / 2)
        self.containerView.frame.origin.x = self.viewPosition

        self.alarmImageNameView.alpha = self.viewAlpha
        self.alarmImageDateView.alpha = self.viewAlpha
        self.alarmSwitchView.alpha = self.viewAlpha

        self.deleteButton.transform = self.buttonTransform

        super.layoutSubviews()
    }

    override func setEditing(_ editing: Bool, animated: Bool) {

        self.viewPosition = editing ? 80 : 0
        self.viewAlpha = editing ? 0 : 1
        self.buttonTransform = editing ? CGAffineTransform(rotationAngle: 90 * (.pi / 180)) : .identity

        let layoutChanges = {
            self.deleteButtonView.frame.origin.x = self.viewPosition - 80
            self.disclosureButtonView.frame.origin.x = self.contentView.bounds.width - (self.viewPosition / 2)
            self.containerView.frame.origin.x = self.viewPosition

            self.alarmImageNameView.alpha = self.viewAlpha
            self.alarmImageDateView.alpha = self.viewAlpha
            self.alarmSwitchView.alpha = self.viewAlpha

            self.deleteButton.transform = self.buttonTransform
        }

        if animated {
            UIView.animate(withDuration: 0.5, delay: 0, options: [.curveEaseInOut], animations: layoutChanges, completion: nil)
        } else {
            layoutChanges()
        }

        super.setEditing(editing, animated: animated)
    }

}

It works very well, also when i'm scrolling, but if I scroll to the very bottom, suddenly the self.containerView seems to ignore the frame.origin.x value, which the layoutSubviews() function provided: YouTube-video of the animations

I don't know, why it's not working, if I set a breakpoint in the layoutSubviews() function, it stops even at the last cells and if I print out the value of self.containerView.frame.origin.x it's also correct.

Edit: If it's important, here my implementation of the cellForRowAtIndexPath:

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

    let alarmProperties = self.alarms[indexPath.row]

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
    cell.delegate = self
    cell.selectionStyle = .none

    cell.alarmTimeLabel.text = "\(alarmProperties.hour):\(alarmProperties.minute)"
    cell.alarmSwitch.isOn = alarmProperties.isActive


    return cell
}
Josef Zoller
  • 921
  • 2
  • 8
  • 24

0 Answers0