iOS:更改UserInteractionEnabled后,一个特定单元格不会从编辑模式切换回来

时间:2014-01-17 15:00:20

标签: ios objective-c uitableview user-interaction

这个问题确实让我疯狂......现在已经好几个小时了。我有一个自定义单元格的表视图。此错误主要发生在ENDING编辑模式时,活动单元格(此单元格的文本字段中的光标)隐藏在键盘后面或滚动到当前可见屏幕之外。然而,它也会在其他时间发生,但更少见。好的,如果我结束编辑模式,这个单元格不会切换回来,删除符号等仍然可见。如果我从活动单元格滚动然后返回以便返回查看,则单元格会正确显示。 而这个问题实际上只发生在我更改此属性UserInteractionEnabled时。如果我发表评论,问题就消失了。但我需要在正常模式下禁用它们,因此整个单元格都可以选择反应。

enter image description here

无论如何,这是一个问题,当前屏幕之外的单元格在切换回时立即处于非编辑模式,并且不能按原样平滑过渡。

好的,这是我的代码。

表视图,更改编辑/非编辑:

- (IBAction) editTable:(id)sender
{
    if(self.editing)
    {
        //Change to editing no
        [super setEditing:NO animated:YES];
        [self.tableView setEditing:NO animated:YES];
        //resigns first responder for all textfields
        [self.view endEditing:YES];

        //Remove Done button and exchange it with edit button
        [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit", nil)];
        [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain];
        self.navigationItem.leftBarButtonItem = nil;
        [self.navigationItem setHidesBackButton:NO animated:YES];
        [((AppDelegate *)[[UIApplication sharedApplication] delegate]) saveContext];
        self.suspendAutomaticTrackingOfChangesInManagedObjectContext = NO;
    } else {
        //Change to editing mode
        [super setEditing:YES animated:YES];
        [self.tableView setEditing:YES animated:YES];

        //Exchange the edit button with done button
        [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done", nil)];
        [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
        [self.navigationItem setHidesBackButton:YES animated:YES];

        //And insert instead of the back button an add button
        UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonAction:)];
        [self.navigationItem setLeftBarButtonItem:addButton];

        //[self.tableView layoutIfNeeded];
    }
}

文本字段确实结束了编辑:

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag inSection:0];
    MainCategory *mainCategory = [self.fetchedResultsController objectAtIndexPath:indexPath];

    if(textField.text != mainCategory.name){
        mainCategory.name = textField.text;
    }

    self.activeField = nil;
}

如果编辑模式更改,我的自定义单元格行为:

   - (void) setEditing:(BOOL)editing animated:(BOOL)animated{
        [super setEditing:editing animated:animated];
        if (editing){
            self.iconButton.hidden = NO;
            self.icon.hidden = YES;
            self.costs.hidden = YES;
            self.disclosureIndicator.hidden = YES;
            self.subcategories.hidden = YES;
            self.title.borderStyle = UITextBorderStyleRoundedRect;
            self.title.backgroundColor = [UIColor whiteColor];
            self.title.userInteractionEnabled = YES;
        }  else if (!editing){
            self.iconButton.hidden = YES;
            self.icon.hidden = NO;
            self.subcategories.hidden = NO;
            self.costs.hidden = NO;
            self.disclosureIndicator.hidden = NO;
            self.title.borderStyle = UITextBorderStyleNone;
            self.title.backgroundColor = [UIColor clearColor];
            self.title.userInteractionEnabled = NO;
            [self resignFirstResponder];
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试- (IBAction) editTable:(id)sender来电[self setEditing:...]而不是

    [super setEditing:NO animated:YES];
    [self.tableView setEditing:NO animated:YES];
    //resigns first responder for all textfields
    [self.view endEditing:YES];

    //Change to editing mode
    [super setEditing:YES animated:YES];
    [self.tableView setEditing:YES animated:YES];

如果您的类不是UITableViewController的子类,那么您需要实现:

- (void) setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}