【问题标题】:Not Sure How to Update Bool Values in Plist from UISwitch in UITableViewCell不确定如何从 UITableViewCell 中的 UISwitch 更新 Plist 中的 Bool 值
【发布时间】:2012-09-20 16:45:38
【问题描述】:

我有一个有四行的表,每行都有一个标题和开关作为附件。我希望在点击相应的开关时更新 plist 布尔值。

列表:

<array>
    <dict>
        <key>Bools</key>
        <array>
            <false/>
            <false/>
            <false/>
            <false/>
        </array>
        <key>Strings</key>
        <array>
            <string>String0</string>
            <string>String1</string>
            <string>String2</string>
            <string>String3</string>
        </array>
    </dict>
</array>

以下是数据源方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; {
return [allergens count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; {
return [[[allergens objectAtIndex: section] objectForKey: @"Strings"] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; {
return @"Header";
}

这是我的开关,带有选择器:switchToggled

UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchView;
[switchView addTarget:self action:@selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside];

下面是 IBAction 方法。我可以成功地让switch tapped 出现在日志中,所以我知道一切正常。我只是不确定如何更新正确的布尔值。

- (IBAction)switchToggled:(id)sender {
NSLog(@"switch tapped");
}

感谢任何帮助!谢谢。

【问题讨论】:

  • 按下开关后需要更新模型。并使用此模型更新开关状态。

标签: xcode uitableview boolean plist uiswitch


【解决方案1】:

当我发布这篇文章时,我对编程非常陌生,我不知道我无法在运行时以编程方式编辑主包中的 plist。我现在明白,要达到我想要的效果,我应该将起始 plist 保存到应用程序文档目录并从中工作。我使用了this 文章来帮助格式化将数据保存到文档目录和从文档目录加载数据的过程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    相关资源
    最近更新 更多