2013-02-15 28 views
2

我具有由数组键从一个plist中从这个代码填充一个UITableView:如何使用commitEditingStyle从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> 

任何帮助将是伟大的!

+0

你没有更新删除后的plist之前更新viewerKeys。将字典写回档案 – 2013-02-15 09:19:22

回答

1

你的问题是你只是从字典中删除该对象。您的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]; 

}

+0

这工作 - 非常感谢你! – 2013-02-15 09:29:26

+0

@ChrisByatt文件操作非常昂贵。每次从文件中读取和写入都不好。你可以考虑一个备用。维护案例词典中的缓存以存储详细信息。只读/写一次到文件。在其他情况下使用该字典。对于共享,您可以使用Singleton类 – 2013-02-15 09:34:45

+0

我知道这并不理想,但是这些操作很少会发生,因为应用程序的想法是一次而不是再次配置。 – 2013-02-15 09:58:12