简体   繁体   中英

UITableView slide not editable row with editable rows Xcode iPhone

I'm developing an iPhone app and I have one problem. I have a UITableView with a few editable rows(

 -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ )

and one not editable row. If I click edit, the editable rows slide a bit to the right and there is a red round button at the left of it, but the not editable row doesn't slide at all. Is there a way to also slide it to the right but without the red button at the left? It doesn't look that nice at the moment. I hope someone can help me with this:)

I am not sure if it's a good idea to change the default behavior of the tableView. But if you really want to, you could eg use indentation.

// Might be target of button
- (void) setEditingMode
{
    tableView.editing = YES;
    [tableView reloadData];
}

// Might be target of button
- (void) resetEditingMode
{
    tableView.editing = NO;
    [tableView reloadData];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = ...;
    ....
    Boolean cellIsEditable = ...;
    if( tableView.editing && !cellIsEditable )
    {
        cell.indentationWidth = ...; // (please experiment to find the exact value)
        cell.indentationLevel = 1;
    }
    else
    {
        cell.indentationLevel = 0;
    } 
}

Subclass the UITableViewCell and slide the uneditable row yourself.

@interface MyHistoryTableViewCell : UITableViewCell
@end

@implementation MyHistoryTableViewCell : UITableViewCell

#define CELL_SLIDE_WIDTH    32  // found empirically
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    if ( self.editingStyle == UITableViewCellEditingStyleNone ) { // not editable
        CGRect frame = self.frame;
        UITableView *tableView = ((UITableView *)(self.superview));
        if ( tableView.editing ) { // going to editing mode
            frame.origin.x = CELL_SLIDE_WIDTH;
            frame.size.width = tableView.frame.size.width - CELL_SLIDE_WIDTH;
        } else {  // ending editing
            frame.origin.x = 0;
            frame.size.width = tableView.frame.size.width;
        }
        [UIView animateWithDuration:0.3 animations:^{ // match the tableView slide duration
            self.frame = frame;
        }];
    }
}
@end

If you have subviews that need to be anchored to the right of the cell (eg, a button that should not slide), then do

mySubview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; // anchors to the right margin

Also, you have to be creative when setting the subview's frame.origin.x. I experimented with many values until I found something that worked (the value made no sense to me).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM