0

我是 x-code 的新手,我想知道:是否可以通过模态 segue 保留复选标记?

我会在我的清单上打勾:

在此处输入图像描述

但是,当我按下完成然后以模态转场返回屏幕时,它会显示为:

在此处输入图像描述

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

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

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

1 回答 1

0

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

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

于 2014-03-14T18:13:13.397 回答