UITableView中的自定义单元格在滑动后立即移动,以进行编辑 [英] Custom cell in UITableView shifts right after swipe to edit

查看:330
本文介绍了UITableView中的自定义单元格在滑动后立即移动,以进行编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想要一个简单的UITableView与向左滑动删除的能力。一切正常,除了对textview在我的原型单元格中的正确约束似乎被移动后,刷卡删除。这是我的表的代码:

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

    }
}

它是什么样子,我已经刷过左侧编辑,然后刷回右侧顶部单元格(stackoverflow不会让我使用图片)。注意,每个单元格中的灰色文本框和标签的右侧不再对齐。单元格中的灰色文本框具有-8的权限约束,所以我也困惑为什么在其他单元格的文本框上有任何边距。

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.

感谢任何帮助,你可以给我,我还是相当新的Swift!我试图在堆栈溢出时找到这样的问题,我已经空了。

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.

推荐答案

一个解决这个问题的方法,并认为我会在这里发布,以防任何其他人遇到同样的问题。

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

它仍然像XCode中的一个错误,我可以不要想到任何时候你可能想要上述的行为。基本上,如果原型单元格中的文本框约束在Pin自动布局菜单中设置为约束边距,那么在您滑动以删除并滑动之后,右侧水平约束将被重置(根据我可以告诉)

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!

这篇关于UITableView中的自定义单元格在滑动后立即移动,以进行编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆