首页 文章

表格编辑完成后,UITwitch在UITableViewCell中仍然略微可见

提问于
浏览
3

在表格编辑模式关闭后, editingAccessoryVieweditingAccessoryView 仍然显示有一个奇怪的问题 .

目前我正在使用 UISwitch 作为编辑附件视图,并且当按下导航栏的编辑按钮时,让表格视图处理用动画移动编辑视图的开/关屏幕 .

几乎所有的编辑附件视图都在屏幕上正确显示动画,但是总有两个不能完全脱离屏幕,然后当单元格出列并重新使用时它们会被重用,以便在滚动时显示它们 .

有没有人看过这个或者我在这里缺少什么?

我正在设置这样的单元格:

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

我已经尝试重写 setEditing 方法在延迟后重新加载表数据以允许动画完成,但它只在有时工作

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

这是一个屏幕截图:

Bug image

2 回答

  • 0

    旧线程,但在Xcode 7中使用UIStepper时遇到同样的问题 . 我的解决方案是将它嵌入到左侧和右侧有一些填充的UIView中,然后将父视图设置为editingAccessoryView . 它有效,而不是一个黑客 .

  • 1

    这感觉就像是一个黑客,所以希望其他人有一个更好的解决方案:

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

相关问题