gpt4 book ai didi

iOS - 如何从 commitEditingStyle 获取表格行的标签

转载 作者:行者123 更新时间:2023-11-29 13:18:51 25 4
gpt4 key购买 nike

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

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

我正在使用这段代码来加载字典:

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

但显然我缺少它们的 key ,因为我不知道如何以编程方式获取它。有什么建议吗?

干杯,

克里斯

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

最佳答案

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

您必须有某种数组,这样您才能为每个单元格获取正确的数据。使用相同的数组获取被删除行的数据。

如果您为您的 cellForRowAtIndexPath: 方法显示一些代码,则可以给出更具体的答案。但是根据indexPath获取数据基本是一样的代码。

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

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

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

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

另外,不要同时调用 reloadDatadeleteRowsAtIndexPaths:。只需调用一个或另一个即可。

还有这一行:

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

应该是:

[dictionary removeObjectForKey:key];

不要使用 stringWithFormat 除非你确实有一个你正在格式化的字符串。

关于iOS - 如何从 commitEditingStyle 获取表格行的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14879912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com