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

标签 ios objective-c uitableview plist

我有一个 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/

相关文章:

ios - UITableView 在 TabBar 下

ios - 在未通过 segue 连接的 UIViewController 和 UITableViewController 之间传递数据

ios - 光滑的圆角 UIView 角,仅底部

ios - 如何使用 Alamofire 关闭证书验证?

ios - Objective c unicode char* 到 NSString

objective-c - 表格重新加载后设置 UITableView FooterView

ios - UITableView 里面的 UICollecionView。如何处理选择?

iOS 类似 Web UI

ios - 更改 UIButton 的标题不起作用

objective-c - 添加对象到 NSDictionary