打开另一个视图并取消后选择单元格

时间:2013-10-23 02:57:33

标签: ios uitableview uiviewcontroller

单击单元格以编辑对象后,单元格将打开另一个VC以允许编辑,但如果用户单击已取消,则单元格仍处于选中状态,并且很快就会出现问题并崩溃。

细胞编辑:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ([segue.identifier isEqualToString:@"addAssignment"])
    {
        AddEditViewController *addEditController = segue.destinationViewController;
        [addEditController setOtherdelegate:self];
    }


   if ([segue.identifier isEqualToString:@"edit"]) {
    UITableViewCell *cell = sender;
    AddEditViewController *addEditController = segue.destinationViewController;
    [addEditController setOtherdelegate:self];
    addEditController.edit = YES;
    AssignmentInfo *a = [self.alist objectAtIndex:[self.tableView indexPathForCell:cell].row];
    addEditController.assignmentEditing = a;

    [self.alist removeObject:a];
    [self.tableView reloadData];
    NSString *filePath = [self dataFilePath];
    [NSKeyedArchiver archiveRootObject:self.alist toFile:filePath];
    }
}

取消按钮按下:

- (IBAction)addAssignmentCancelButtonPressed:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

如果需要整个项目: http://abdelelrafa.com/AssignmentAppTwo.zip

3 个答案:

答案 0 :(得分:1)

首先,您需要删除此行:

[self.alist removeObject:a]; 

您的下一步:-(void)newAssignment:(AssignmentInfo *)assignment 删除:

  [self.tableView reloadData];

并添加:

 [self.tableView reloadData];

-(void)deletedAssignment:(AssignmentInfo *)assignment

最后一句:-(void)newAssignment:(AssignmentInfo *)assignment 删除:

  NSString *filePath = [self dataFilePath];
[NSKeyedArchiver archiveRootObject:self.alist toFile:filePath];

并添加:

NSString *filePath = [self dataFilePath];
[NSKeyedArchiver archiveRootObject:self.alist toFile:filePath];

-(void)deletedAssignment:(AssignmentInfo *)assignment

答案 1 :(得分:0)

您正在呼叫

[self.alist removeObject:a]; 
每次单击一个单元格时

方法。因此,当您删除对象时再次选择单元格时,将获得数组越界。

我不太确定您希望应用程序如何流动,但您可以致电:

[self.tableView reloadData];

删除'a'对象后刷新表。这样,该表将显示最新的数据。

否则,我建议你不要调用removeObject,但实际上将对象传递给AddEditViewController并在那里修改它。

答案 2 :(得分:0)

检查UITableViewController -clearsSelectionOnViewWillAppear

的值

该设置默认为YES,因此如果错误并且您未在某处手动设置它,则可能会导致View或View Controller生命周期混乱。

相关问题