简体   繁体   English

编辑tableView时,自定义单元格imageView移到左侧

[英]Custom cell imageView shifts to left side when editing tableView

I have a tableView that I need to be able to delete items from, however, when I put it back from editing mode, the imageView from my custom cell gets shifted over to the left side. 我有一个tableView,我需要能够从其中删除项目,但是,当我从编辑模式放回tableView时,自定义单元格中的imageView会移到左侧。 Here's some relevant code and images: 以下是一些相关的代码和图像:

在此处输入图片说明 在此处输入图片说明

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! NotificationsTableViewCell
    cell.storiesLabel.text = stories[indexPath.row].name
    cell.selectionStyle = UITableViewCellSelectionStyle.None
    cell.shouldIndentWhileEditing = false

    if stories[indexPath.row].notificationBool == true {
        cell.notificationImageView.image = UIImage(named: "notifications_filled.png")
    }
    else {
        cell.notificationImageView.image = UIImage(named: "notifications.png")
    }

    return cell
}

func editFollowedStoriesTableView() {
    if tableView.editing == true {
        tableView.setEditing(false, animated: false)
    }
    else {
        tableView.setEditing(true, animated: false)
    }
}

 func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {

    if(self.tableView.editing){

        return UITableViewCellEditingStyle.Delete

    }
    return UITableViewCellEditingStyle.None

}

How can I prevent the second picture from happening? 如何防止第二张图片发生? Any help is very much appriciated! 任何帮助都非常有用!

You have some conflicting auto layout constraints. 您有一些相互矛盾的自动布局约束。 You need to delete one of the following constraints on the UIImageView: 您需要删除UIImageView上的以下约束之一:

Width
Leading Space to: Label
Trailing space to: Superview

Here, The leading space and trailing space constraints define the width of the imageView. 在此,前导空间和尾随空间约束定义了imageView的宽度。 Then you added the width constraint that does the same thing. 然后,您添加了执行相同操作的宽度约束。 Now if you want your image view to have a fixed width delete either the leading space or trailing space constraint. 现在,如果您希望图像视图具有固定的宽度,请删除前导空间或尾随空间约束。 If you don't need to have a fixed width, delete the width constraint. 如果不需要固定宽度,请删除宽度约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM