用模态瞄准保留复选标记

时间:2014-03-14 18:09:40

标签: objective-c list segue modalviewcontroller checkmark

我是x-code的新手,我想知道:虽然你经历了一个模态segue,是否有可能保留复选标记?

我会查看我名单上的一些方框:

enter image description here

但是当我按完了然后再回到带有模态segue的屏幕时,就会出现这样的情况:

enter image description here

虽然我以模态方式更改了视图,是否可以保留这些复选标记?

我有这段代码来创建复选标记:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.toDoItems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ListPrototypeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NewItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
    cell.textLabel.text = toDoItem.itemName;
    if (toDoItem.completed) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NewItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
    tappedItem.completed = !tappedItem.completed;
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

1 个答案:

答案 0 :(得分:0)

当您弹出或关闭视图控制器时,该视图控制器消失。但是,您有几个选项可以记住视图控制器所处的状态。最简单的方法可能是存储一个全局变量,可能是NSArray,以记住已检查的项目。然后当你加载这个视图控制器时,你可以检查" NSArray中存在的任何项目。

请注意,此方法仅适用于应用程序打开的生命周期。如果他们关闭了应用程序,它就会消失。如果你想保持"检查"下次他们打开应用时,您需要将其存储在NSUserDefaults中 - 数据可用,直到应用程序从手机中删除。