我有一个 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];
}