【问题标题】:Custom cell in UITableView shifts right after swipe to editUITableView 中的自定义单元格在滑动编辑后立即移动
【发布时间】:2015-07-28 23:56:58
【问题描述】:

我只想要一个能够向左滑动删除的简单 UITableView。一切正常,除了在我的原型单元格中的 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 还是很陌生!我试图在堆栈溢出时找到类似这个问题的任何内容,但结果是空的。

【问题讨论】:

    标签: ios swift uitableview interface-builder constraints


    【解决方案1】:

    好的,所以我找到了解决此问题的方法,并认为我会在这里发布,以防其他人遇到同样的问题。

    在我看来,它仍然是 XCode 中的一个错误,因为我想不出任何时候您可能想要上述行为。基本上,如果原型单元格中的文本框约束在 Pin 自动布局菜单中设置为“约束到边距”,那么在您滑动删除然后滑动后,右侧的水平约束将随机重置(据我所知)返回。

    当您添加这些约束时,只需取消选中对边距的约束,它应该可以解决这个问题!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-09
      • 1970-01-01
      • 2011-11-27
      相关资源
      最近更新 更多