Custom cell in UITableView shifts right after swipe to edit

I just want a simple UITableView with the ability to slide left to delete. Everything works okay except the right constraint on the textview in my prototype cell seems to get shifted after swiping to delete. Here’s my code for the table:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    //Uses prototype cell from Interface Builder called "CommentTableCell"
    let tableCell = tableView.dequeueReusableCellWithIdentifier("CommentTableCell", forIndexPath: indexPath) as! CommentTableCell
    tableCell.userInteractionEnabled = true
    tableCell.selectionStyle = .None
    //Sets the text for the cells in the comment table
    tableCell.commentText.text = comments[indexPath.row]
    tableCell.timeLabel.text = commentTimes[indexPath.row]

    return tableCell

}


//As many rows in the table as there are comments
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return comments.count

}

//Allows the user to delete comments
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    if (editingStyle == UITableViewCellEditingStyle.Delete) {

        comments.removeAtIndex(indexPath.row)
        commentsTable.deleteRowsAtIndexPaths([indexPath],  withRowAnimation: UITableViewRowAnimation.Automatic)
    }

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.layer.borderWidth = 0.75
    self.view.layer.borderColor = borderColor.CGColor
    self.view.layer.cornerRadius = 5.0

    //Gets rid of the line between comment cells
    commentsTable.separatorStyle = UITableViewCellSeparatorStyle.None
    commentsTable.backgroundView = nil

    //Sets the height of the row to fit text boxes
    self.commentsTable.estimatedRowHeight = self.commentsTable.rowHeight
    self.commentsTable.rowHeight = UITableViewAutomaticDimension

    }
}

This is what it looks like after I’ve swiped left to edit and then swiped back right on the top cell (stackoverflow won’t let me use pictures yet). Note the right sides of the gray text boxes and labels in each cell are no longer aligned. The gray text box in the cells has a right constraint of -8 so I’m also confused why there’s any margin on the other cell’s text boxes at all.

  • 'NSInvalidArgumentException', reason: 'Unable to parse constraint format'
  • iOS 8 - change character spacing on UILabel within Interface Builder
  • What is the XCode Interface Builder “Document” Section
  • Unable to connect IBOutlet from storyboard to UIView subclass
  • xcode interfacebuilder Objects linked to custom class in Inspector?
  • Xcode auto layout constraints - why defaulting to negative padding?
  • Thanks for any help you can give me, I’m still fairly new to Swift! I’ve tried to find anything like this question on stack overflow and I’ve come up empty.

    Solutions Collect From Internet About “Custom cell in UITableView shifts right after swipe to edit”

    Okay so I found a way to fix this and thought I’d post here in case anyone else runs into the same problem.

    It still seems like a bug in XCode to me as I can’t think of any time you might want the behavior described above. Basically, if the text box constraints in the prototype cell are set to “constrain to margins” in the Pin auto layout menu then the right horizontal constraint will be reset (as far as I can tell) randomly after you slide to delete and then slide back.

    Just uncheck constrain to margins when you add those constraints and it should fix this problem!