10

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;
  }
Surfer
  • 1,370
  • 3
  • 19
  • 34
jwknz
  • 6,598
  • 16
  • 72
  • 115
  • Are your labels added directly to the cell or did you add them to the cell's `contentView`? They should be in the `contentView` for them to automatically adjust when the cell goes to edit mode or other cell decorations are added. – rmaddy Dec 02 '12 at 03:42
  • I created a new Custom Class and dragged them into that from Storyboard - I think that means they are directly on the cell right?? – jwknz Dec 02 '12 at 03:44
  • I have added the code for my custom cell - I have not added anything in the .m file - haven't needed to up til now. – jwknz Dec 02 '12 at 03:45
  • 1
    I don't use Interface Build so I have no idea how to do this. I do everything in code. Sorry. – rmaddy Dec 02 '12 at 03:50
  • Code is fine :-) I can use both, code would better so I understand what happens under the hood:-) – jwknz Dec 02 '12 at 03:52
  • What is the autoresizing mask used for your labels? It needs to, for example, have a flexible left margin to move in from the right when the delete button appears. – jrturton Dec 02 '12 at 08:30

3 Answers3

9

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];
Fredrik
  • 1,282
  • 12
  • 15
  • I have added the code that you said at the top (3rd lot of code) is that the right place? It still does not work. But in the TableView I still set the textlabels as I have right?? – jwknz Dec 02 '12 at 07:53
  • Sorry, if you look at the original question, I have split of my submitted code in 3 segments. the 3rd one is the one I am referring to. it is right at the bottom of the question. – jwknz Dec 02 '12 at 08:17
  • Ok you use xib's. Didn't know that, well here's how to do it with xibs or without. http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7 – Fredrik Dec 02 '12 at 08:19
  • Yeah I have gone through that but i am none the wiser:-) – jwknz Dec 02 '12 at 08:23
  • Ok I went the code way and re-created the layout without Storyboard. Thanks a lot for you help - answer accepted:-) – jwknz Dec 02 '12 at 09:57
  • @JeffKranenburg: I am facing the same kind of issue with iOS 7, it is moving cell.textLabel to left side. Any solution to avoid that. I am using storyboard. – Mrunal Nov 14 '13 at 09:59
1

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];
    }
}
Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
  • You have to just change the frame positions in both of the methods. In `will..` method, you have to move the label right, and in `did..` method, you have to move the label left. – Ilanchezhian Dec 02 '12 at 04:13
  • ok - I got it working, however the label does not animate with it, it just flicks to the new position. I have tried [UIView beginAnimations:nil context:NULL]; but there is to much of a delay to make it look natural. – jwknz Dec 02 '12 at 04:59
  • Sorry I just saw your answer - had dinner:-) The only way I can move the labels is by using - UITableViewCellStateShowingEditControlMask, not the once you have given me. However it also comes with the delay. Your answers seem to make sense though and have gotten me so far. – jwknz Dec 02 '12 at 06:40
  • It should be `if (state == UITableViewCellStateEditingMask)` in the `willTransitionToState` method. But thanks for the answer, up vote. – emotality Sep 01 '14 at 19:27
0

Its a hack but the only thing that worked for me was putting in a hidden image and constraining my labels against it.

For instance:

I had a UILabel within a UITableViewCell that wasn't moving with the cell when I animated my table on and off the screen.. but other cells worked just fine. I tried everything.

I ended up putting an image on the left side of the label (like the other cells had), and adding constraints from the label to the fixed width image, and a constraint from the left of the image to the container.

This fixed my issue, and now the label animates with the cell.

I'm not sure what jenky layout is going on with labels, but I tried all of the content modes, and played with settings for well over two hours and nothing else worked.

TheJeff
  • 3,665
  • 34
  • 52