3

关闭表格编辑模式后,我有一个奇怪的问题editingAccessoryViewUITableViewCell

目前,我正在使用 aUISwitch作为编辑附件视图,并在按下导航栏的编辑按钮时让表格视图处理在屏幕上移动编辑视图的动画。

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

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

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

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

这是一个屏幕截图:

错误图片

4

2 回答 2

1

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

于 2015-09-21T21:31:21.450 回答
0

这感觉就像一个 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;
}
于 2013-03-02T15:54:28.153 回答