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

时间:2013-02-15 09:14:06

标签: 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;
}

我正在尝试在表视图中的每一行上启用滑动删除,当然这意味着我必须删除该行以及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,我收到了这个错误:

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的样子:

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

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:1)

您的问题是您只是从字典中删除该对象。您的viewerKeys仍然保留已移除的对象键。在致电viewerKeys

之前,您需要更新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];

}