Prevent indentation of UITableViewCell (contentView) while editing

前端 未结 9 752
心在旅途
心在旅途 2020-12-05 02:38

Is it possible to keep the contentView.frame always the same, regardless of tableView.editing? I already tried to override layoutSubviews

相关标签:
9条回答
  • 2020-12-05 02:48

    I had the same trouble with my plain style table even with shouldIndentWhileEditingRowAtIndexPath returning NO

    On my side, I encounter the issue because my first row should not be deleted and the rest of the table view cells should.

    I added this second method and it worked:

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if (indexPath.row == 0 && indexPath.section == 0) return NO;
    
        return YES;    
    }
    

    Hope it helps

    0 讨论(0)
  • 2020-12-05 02:49

    Use the UITableViewDelegate method:

    - (BOOL)tableView:(UITableView *)tableView 
            shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath 
    {
    
        return NO;
    }
    

    This will work for both grouped and non-grouped UITableView types. However, if you have a grouped tableview, you can use this property on the cell:

    cell.shouldIndentWhileEditing = NO;
    
    0 讨论(0)
  • 2020-12-05 02:56

    Just came across this as I was researching the same problem.

    A very easy solution is to set the indentation level to a negative number - it is a signed integer after all.

    A [cell setIndentationLevel:-3] worked perfectly for me, given a default indentation width of 10, to move the label back to the left in a plain table.

    0 讨论(0)
  • 2020-12-05 03:04

    Luckily, iOS is perfectly equipped to keep whatever value constant.

    This (ugly) piece of code will keep the frame fixed to whatever value has origin.x==0. This is easily adapted to your particular needs.

    // put this in your UITableViewCell subclass
    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        NSLog(@"observed value for kp %@ changed: %@",keyPath,change);
        if ( [keyPath isEqual:@"frame"] && object == self.contentView )
        {
            CGRect newFrame = self.contentView.frame;
            CGRect oldFrame = [[change objectForKey:NSKeyValueChangeOldKey] CGRectValue];
            NSLog(@"frame old: %@  new: %@",NSStringFromCGRect(oldFrame),NSStringFromCGRect(newFrame));
    
            if ( newFrame.origin.x != 0 ) self.contentView.frame = oldFrame;
        }
    }
    
    // add the cell as an observer for frame changes, somewhere in initialization:
    [self.contentView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionOld context:nil];
    
    // IMPORTANT: remove the cell as an observer in -dealloc:
    [self.contentView removeObserver:self forKeyPath:@"frame"];
    

    This will only allow the frame to change to values with an origin.x==0. Remove the NSLog() lines for Release builds.

    I have tested this for a few minutes and haven't seen any side effect.

    0 讨论(0)
  • 2020-12-05 03:04

    You can always get a tableviewcell with an indexpath. Using that tableviewcell reuseidentifier, You can avoid the tableview cell content size to be resized or not. I had a requirement to implement the similar kind of functionality to avoid resizing of seperate cells. PFB the code.

    -(BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BOOL shouldIndentWhileEditingRow = NO;
    
    UITableViewCell *lTableViewCell = [tableView cellForRowAtIndexPath:indexPath];
    
    /*Change the position of the target rect based on Sending messages or Receiving messages*/
    if ([lTableViewCell.reuseIdentifier isEqualToString:@"SendingChatCellIdentifier"]) {
    
        shouldIndentWhileEditingRow = NO;
    
    }else if ([lTableViewCell.reuseIdentifier isEqualToString:@"ReceivingChatCellIdentifier"]){
    
        shouldIndentWhileEditingRow = YES;
    }
    
    return shouldIndentWhileEditingRow;
    }
    
    0 讨论(0)
  • 2020-12-05 03:05

    You will have to override layoutSubviews to do this. It applies for the level of custom indentation so it does for none.

    Please have a look at How can I change the amount of indentation on my custom UITableViewCell while editing?. I provided an example how to change the level of indentation. Though it didn't work 100% for the OP it worked in my example app. I think this will point you in the right direction.

    0 讨论(0)
提交回复
热议问题