Custom UITableViewCell in edit mode does not move my UILabels

跟風遠走 提交于 2019-11-30 09:13:40

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];

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];
    }
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!