gpt4 book ai didi

ios - 如何使用 commitEditingStyle 从 plist 中删除数组?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:45:04 25 4
gpt4 key购买 nike

我有一个 UITableView,它通过以下代码由 plist 中的数组键填充:

-(void)viewWillAppear:(BOOL)animated
{
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:(NSString *)plistPath];
viewerKeys = [dictionary allKeys];

[self.tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ViewerName"];

UILabel *label = (UILabel *)[cell viewWithTag:1000];
label.text = [viewerKeys objectAtIndex:indexPath.row];
return cell;
}

我正在尝试启用滑动以删除 TableView 中的每一行,当然这意味着我必须删除该行以及 plist 中的数组。到目前为止,这是我尝试过的:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:(NSString *)plistPath];

NSString *key = [viewerKeys objectAtIndex:indexPath.row];
[dictionary removeObjectForKey:[NSString stringWithFormat:@"%@", key]];


NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
}

但是它没有将数据写回 plist,我得到了这个错误:

无效更新:第 0 节中的行数无效。更新 (2) 后现有节中包含的行数必须等于更新 (2) 前该节中包含的行数,加上或减去从该部分插入或删除的行数(0 插入,1 删除),加上或减去移入或移出该部分的行数(0 移入,0 移出)。

这是我的 plist 的样子:

<plist version="1.0">
<dict>
<key>(null)</key>
<array>
<string>Username</string>
<string>Password</string>
<string>http://www.google.com</string>
<string>/whatever</string>
</array>
<key>Hello</key>
<array>
<string>admin</string>
<string></string>
<string>https://www.whatever.com</string>
<string>/things</string>
</array>
</dict>
</plist>

任何帮助都会很棒!

最佳答案

你的问题是你只是从字典中删除了那个对象。您的 viewerKeys 仍然保留已删除对象的键。您需要在调用 deleteRowsAtIndexPaths:

之前更新 viewerKeys
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:(NSString *)plistPath];

NSString *key = [viewerKeys objectAtIndex:indexPath.row];
[dictionary removeObjectForKey:[NSString stringWithFormat:@"%@", key]];


[dictionary writeToFile:plistPath atomically:YES];
viewerKeys = [dictionary allKeys];

NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];

关于ios - 如何使用 commitEditingStyle 从 plist 中删除数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14891351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com