Jeff Kranenburg Jeff Kranenburg - 1 month ago 7
iOS Question

Custom UITableViewCell in edit mode does not move my UILabels

This is doing my head in:-)

I have a fully functional CoreData Populated

UITableView
inside a
UIViewController
and I have successfully implemented the "Swipe to Delete option" (which is easy) and I can also delete single instances with an edit button where the red circle thingy comes up.

My problem is, and I think it is because I have a CustomCell, that when I press the edit button the
UILabels
do not move to the right.

I have tried using
-(void)layoutSubViews
and a few others, but nothing works.

I have posted my code for my
cellForRowAtIndexPath
. This is part of a note section in my app. This code works, I just need to know How to move the labels when I go into Edit mode??

Thank you for the tips and advice:-)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";

MNCustomCell *cell = [_mainTableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[MNCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}


//cell.textLabel.text = [_tableArray objectAtIndex:indexPath.row];

MNotes *mnotes = [[self fetchedResultsController] objectAtIndexPath:indexPath];

cell.noteTitle.text = mnotes.noteTitleString;
cell.noteSummary.text = mnotes.mainNoteString;

mnotes.createDate = [[NSDate alloc] init];
SORelativeDateTransformer *relativeDateTransformer = [[SORelativeDateTransformer alloc] init];
NSString *relativeDate = [relativeDateTransformer transformedValue:mnotes.createDate];

cell.noteDate.text = relativeDate;


cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;
}


-

//This is the Custom Cell
@interface MNCustomCell : UITableViewCell
{

}

@property (strong, nonatomic) IBOutlet UILabel *noteTitle;
@property (strong, nonatomic) IBOutlet UILabel *noteDate;

@property (strong, nonatomic) IBOutlet UITextView *noteSummary;


@end


-

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code

[self.contentView addSubview:_noteTitle];
[self.contentView addSubview:_noteSummary];
[self.contentView addSubview:_noteDate];

}
return self;
}

Answer

The other solution will probably work but this way will do the animation automatically for you. MNCustomCell is not going to re-layout the view depending on the current state of the cell, but if you add your label to the contentView of the cell, it will.

The following example will move the label so it doesn't interfere with the delete button.

MNCustomCell.m

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 15.0)]];
        mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
        [cell.contentView addSubview:mainLabel];