如何使用commitEditingStyle从plist删除数组? [英] How can I delete an array from a plist using commitEditingStyle?

查看:86
本文介绍了如何使用commitEditingStyle从plist删除数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UITableView which is populated by the array keys from a plist via this code:

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

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

I'm trying to enable a swipe to delete on each row in the table view, and of course this means I have to delete the row as well as the array in the plist. This is what I've tried so far:

- (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中,我得到了这个错误:

However it isn't writing the data back to the plist and I get this error:

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).

这是我的plist的样子:

This is what my plist looks like:

<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:

Your problem is you are removing that object from the dictionary only. your viewerKeys still hold removed objects key. You need to update viewerKeys before calling deleteRowsAtIndexPaths:

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

}

这篇关于如何使用commitEditingStyle从plist删除数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆