我只想要一个简单的 UITableView,可以向左滑动删除。一切正常,除了我的原型(prototype)单元格中 textview 的右约束似乎在滑动删除后移动了。这是我的表格代码:
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是我向左滑动进行编辑然后向右滑动顶部单元格后的样子(stackoverflow 还不允许我使用图片)。请注意,每个单元格中灰色文本框和标签的右侧不再对齐。单元格中的灰色文本框具有 -8 的右约束,所以我也很困惑为什么其他单元格的文本框上有任何边距。
感谢您能给我的任何帮助,我对 Swift 还是很陌生!我试图在堆栈溢出中找到类似这个问题的任何内容,但结果一无所获。
最佳答案
好吧,所以我找到了解决这个问题的方法,我想我会在这里发帖,以防其他人遇到同样的问题。
在我看来,它仍然像是 XCode 中的一个错误,因为我想不出任何时候您可能想要上述行为。基本上,如果原型(prototype)单元格中的文本框约束在 Pin 自动布局菜单中设置为“约束到边距”,那么在您滑动删除然后滑动后,右侧的水平约束将随机重置(据我所知)回来。
添加这些约束时只需取消选中对边距的约束,它应该可以解决此问题!
关于ios - UITableView 中的自定义单元格在滑动后立即移动以进行编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31689067/