Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
  }
like image 664
jwknz Avatar asked Dec 02 '12 03:12

jwknz


2 Answers

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];
like image 146
Fredrik Avatar answered Sep 20 '22 08:09

Fredrik


Implement the following methods in your custom cell class.

- (void)willTransitionToState:(UITableViewCellStateMask)state 

and

- (void)didTransitionToState:(UITableViewCellStateMask)state

and move your label accordingly.

It should be like

- (void)willTransitionToState:(UITableViewCellStateMask)state {

    [super willTransitionToState:state];

    if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask) {
       label.frame = ...
    }
}

Edit:

- (void)willTransitionToState:(UITableViewCellStateMask)state {

    [super willTransitionToState:state];

//    label.hidden = YES;
//    label.alpha = 0.0;
}

- (void)didTransitionToState:(UITableViewCellStateMask)state {

    [super didTransitionToState:state];

    if (state == UITableViewCellStateShowingDeleteConfirmationMask) {

        [UIView beginAnimations:@"anim" context:nil];
        label.frame = leftFrame;
        [UIView commitAnimations];
    } else if (state == UITableViewCellStateDefaultMask) {

        [UIView beginAnimations:@"anim" context:nil];
        label.frame = rightFrame;
        [UIView commitAnimations];
    }
}
like image 35
Ilanchezhian Avatar answered Sep 19 '22 08:09

Ilanchezhian