Custom cell in UITableView is shifted right after swipe for editing

I just want a simple UITableView with the ability to slide to the left for deletion. Everything works fine, except that the correct constraint on the text image in my prototype cell seems to shift after scrolling for deletion. Here is 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 appears to be after I checked to the left to edit and then tossed back at the top cell (stackoverflow won't let me use photos yet). Notice that the right-hand sides of the gray text boxes and labels in each cell are no longer aligned. The gray textbox in the cells has the correct -8 limit, so I'm also confusing why there are any margins in the other textboxes of the cell.

Thanks for any help you can give me, I'm still pretty new to Swift! I tried to find something like this question on Stack Overflow and I came out empty.

+3


source to share


1 answer


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

I still feel like a bug in Xcode as I can't think of you needing the behavior described above. Basically, if the textbox constraints in the prototype cell are set to "constraint margins" in the auto layout Pin menu, then the correct horizontal constraint will be reset (as far as I can tell) randomly after you click to delete and then release.



Just remove the marginal constraints when adding those constraints and fix the problem!

+2


source







All Articles