Custom cell imageView shifts to left side when editing tableView

那年仲夏 提交于 2019-12-13 05:22:52

问题


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. 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!


回答1:


You have some conflicting auto layout constraints. You need to delete one of the following constraints on the UIImageView:

Width
Leading Space to: Label
Trailing space to: Superview

Here, The leading space and trailing space constraints define the width of the 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.



来源:https://stackoverflow.com/questions/32236984/custom-cell-imageview-shifts-to-left-side-when-editing-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!