Custom UITableViewCell is broken when dragged to reorder

I have a custom subclass of UITableView that inserts a separator string as a subtitle, for example:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { CGRect frame = [self bounds]; frame.origin.y = CGRectGetMaxY(frame) - 1; frame.size.height = 1; UIView *separator = [[[UIView alloc] initWithFrame:frame] autorelease]; [separator setBackgroundColor:[UIColor whiteColor]]; [separator setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin]; [self addSubview:separator]; } return self; } 

This draws a beautiful line under my cell. However, when the tableview is in edit mode and I drag it to reorder the cells, this sub-item disappears!

Everything else looks the same, and the opacity of the cell decreases as you drag it, but why will this subview be removed?

+4
source share
2 answers

Performing a drawRect override:

 - (void)drawRect:(CGRect)rect { [super drawRect:rect]; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]); CGContextFillRect(context, CGRectMake(0, CGRectGetMaxY(rect)-.5, CGRectGetWidth(rect), .5)); CGContextFillRect(context, CGRectMake(0, 0, CGRectGetWidth(rect), .5)); CGContextRestoreGState(context); } 
+1
source

Instead of adding a dividing line as a preview, use the default value that has the table view and change the borderColor through the cell's backgroundView layer.

 cell.backgroundView.layer.borderColor = [UIColor blueColor]; 

#import <QuartzCore/QuartzCore.h> , obviously.

Also, if you are using a grouped table view, make sure it matches the outer border:

 tableView.separatorColor = [UIColor blueColor]; 

Make sure the separator line is back again, of course :) (Therefore, do not set the tableView separatorStyle property to none: tableView.separatorStyle = UITableViewCellSeparatorStyleNone )

-1
source

Source: https://habr.com/ru/post/1389527/


All Articles