1

我有一个 UITableView,它由 NSMutableDictionary 中的数组键填充。

为了能够删除这些数组,我需要能够以某种方式获取密钥。我的想法是最简单的就是从行的标签中得到它。

我正在使用此代码加载字典:

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

这从字典中删除数组:

[dictionary removeObjectForKey:];

但显然我错过了它们的关键,因为我不知道如何以编程方式获取它。有小费吗?

干杯,

克里斯

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

cellForRowAtIndexPath:

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

删除代码:

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

    [self.tableView reloadData];

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

1 回答 1

1

永远不要从表格单元格中获取数据。您已经拥有用于填充表格单元格的数据结构,从这些相同的数据结构中获取数据。

您必须有某种数组,以便您可以为每个单元格获取正确的数据。使用相同的数组来获取被删除行的数据。

如果您为您的cellForRowAtIndexPath:方法显示一些代码,则可以给出更具体的答案。但它基本上是相同的代码来获取基于indexPath.

您到目前为止发布的代码仅显示字典。字典不是表格等基于行的结构的良好基础。

更新:根据您的代码cellForRowAtIndexPath:,您只需要:

NSString *key = [viewerKeys objectAtIndex:indexPath.row];

更新 2:为什么要在commitEditingStyle方法中再次加载文件?您需要修改已为表加载的现有数据结构。就目前而言,您拥有表使用的数据结构。然后在 中commitEditingStyle,再次加载数据,从该临时数据中删除一个条目,然后告诉表重新加载。重新加载将使用原始数据结构,而不是这个临时数据。

另外,不要同时调用reloadDataand deleteRowsAtIndexPaths:。只需调用一个或另一个。

而这一行:

[dictionary removeObjectForKey:[NSString stringWithFormat:@"%@", key]];

应该:

[dictionary removeObjectForKey:key];

stringWithFormat除非您确实有要格式化的字符串,否则请勿使用。

于 2013-02-14T17:03:02.953 回答