简体   繁体   中英

Swipe to Delete on a UITableViewCell: cell gets squished

I have a custom tableview cell created programmatically that consists of: a background view with an image 3 labels on the left and hand side one UISwitch on the right hand side. I want to be able to swipe and delete the cell. This works, however, when I swipe to bring up the delete button, the delete button pushes the background view (along with everything else) over as if it's squishing the cell. How do I get it so that the background image doesn't shrink?

I've searched around for this problem and some people said it had to do with autoresizing masks, but that didn't seem to affect anything. I also don't want to manually set the width of the cell in the layoutSubviews method as I saw one suggestion had.

Any ideas? Thanks!

The problem, I'm guessing (since you didn't post any code), is that you added all of these UI Elements as subviews of the cell, ie cell.backgroundColor = [UIColor someColor], [cell addSubview:label1], etc etc.

Here's the thing: A cell's view doesn't know how to resize itself appropriately when things like editing mode occur. However, a cell's contentView, which is a UIView, DOES know how to resize. For this reason, you need to add your labels, switches, etc. as subviews of the contentView, not the cell. Doing this will resize everything appropriately when you enter editing mode.

I just resolved the similar problem. It has nothing to do with the content view . When you added a UIImageView to a cell, either in interface builder, or storyboard, it has all springs and struts set. You will notice that your UILabels only have top and left strut set.

Removing inner springs and right/bottom strut for a UIImageView on a UITableView cell would prevent them from being squished by the swipe to delete button.

UItableview单元格挤压UIImage修复

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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