简体   繁体   中英

UISwitch still slightly visible in UITableViewCell after table editing is complete

I have a strange issue with the editingAccessoryView of a UITableViewCell still showing after the table editing mode is switched off.

Currently I am using a UISwitch as the editing accessory view and letting the table view handle moving the editing view on/off screen with animation when the navigation bar's edit button is pressed.

Almost all of the editing accessory views animate correctly off screen, but there are always two that don't quite make it off screen and then they get reused when the cell is dequeued and reused so they show up during scrolling.

Has anyone else seen this or is there something I am missing here?

I am setting up the cell like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AllStatCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AllStatCell"];

    if (cell.editingAccessoryView == nil) {
        UISwitch *statSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
        [statSwitch addTarget:self action:@selector(saveSwitchState:) forControlEvents:UIControlEventValueChanged];
        cell.editingAccessoryView = statSwitch;
    }

    NSString *statName = [self statNameForIndexPath:indexPath];
    cell.statName.text = statName;
    [(UISwitch *)cell.editingAccessoryView setOn:[self switchValueForIndexPath:indexPath withStatNamed:statName]];

    if (tableView.editing) cell.statValue.hidden = YES;

    return cell;
}

I have tried overriding the setEditing method to reload the table data after a delay to allow for the animations to complete, but it only works sometimes

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    // Insert/delete the stat rows depending on editing mode
    [self rebuildTableDataAnimated:YES];

    double delayInSeconds = 0.5;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        // Trying to fix bug that shows part of a UISwitch on screen still even though editing mode is completed
        [self.tableView reloadData];
    });
}

Here's a screen shot:

错误图像

Old thread, but having the same problem in Xcode 7 with a UIStepper. My solution was to embed it in a UIView with some padding on the left and right, then set the parent view to the editingAccessoryView. It works, and is less of a hack.

This feels like such a hack, so hopefully someone else has a better fix for it:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AllStatCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AllStatCell"];

    NSString *statName = [self statNameForIndexPath:indexPath];

    // Hack fix for a bug that shows part of a UISwitch on screen still even though editing mode is completed
    // Configure stat on/off switches
    cell.editingAccessoryView = [self cellEditingAccessoryViewForEditing:tableView.editing];
    if (tableView.editing) [(UISwitch *)cell.editingAccessoryView setOn:[self switchValueForIndexPath:indexPath withStatNamed:statName]];

    cell.statName.text = statName;
    [cell.statValue setHidden:tableView.editing];

    return cell;
}

// Hack fix for a bug that shows part of a UISwitch on screen still even though editing mode is completed
- (UISwitch *)cellEditingAccessoryViewForEditing:(BOOL)tableIsEditing
{
    if (tableIsEditing) {
        UISwitch *statSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
        [statSwitch addTarget:self action:@selector(saveSwitchState:) forControlEvents:UIControlEventValueChanged];
        return statSwitch;
    }
    return nil;
}

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