【问题标题】:UISwitch still slightly visible in UITableViewCell after table editing is complete表格编辑完成后,UISwitch 在 UITableViewCell 中仍然略微可见
【发布时间】:2013-02-16 23:52:19
【问题描述】:

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

目前我使用UISwitch 作为编辑附件视图,并让表格视图处理在按下导航栏的编辑按钮时通过动画打开/关闭屏幕的编辑视图。

几乎所有的编辑附件视图都能在屏幕外正确显示动画,但总有两个不能完全离开屏幕,然后当单元格出列并重用时它们会被重用,因此它们会在滚动期间显示出来。

p>

有没有其他人看到这个或者我在这里遗漏了什么?

我正在这样设置单元格:

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

这是一个屏幕截图:

【问题讨论】:

  • 我的第一次尝试是在您完成编辑后重新加载表格。
  • @flexaddicted 不幸的是,这不起作用
  • 对不起,我没看到。像[tableView setEditing:NO animated:YES]; [tableView reloadData]; 这样重新加载呢?你在后台执行一些计算吗?
  • 此外,您可以尝试(出于测试目的)使用您自己的 bool 属性来查看表是否处于编辑模式,并对此进行检查。例如if (isEditingMode) cell.statValue.hidden = YES;
  • 没有什么是应该的。我最终找到了一个修复它,但感觉就像一个黑客。如果我们不再处于编辑模式,我只是将单元格的编辑附件视图设置为 nil,然后我重新加载数据

标签: iphone ios uitableview editing uiswitch


【解决方案1】:

旧线程,但在带有 UIStepper 的 Xcode 7 中存在同样的问题。我的解决方案是将它嵌入到 UIView 中,左右两边有一些填充,然后将父视图设置为editingAccessoryView。它有效,而且不是黑客攻击。

【讨论】:

    【解决方案2】:

    这感觉就像一个 hack,所以希望其他人有更好的解决方法:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2011-07-15
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 2012-09-07
      相关资源
      最近更新 更多