iOS-如何从CommittedItingStyle获取表行的标签
我有一个UITableView,它由NSMutableDictionary中的数组键填充 为了能够删除这些数组,我需要能够以某种方式获取密钥。我的想法最简单的是从这一行的标签上得到它 我正在使用以下代码加载字典:iOS-如何从CommittedItingStyle获取表行的标签,ios,objective-c,arrays,plist,nsmutabledictionary,Ios,Objective C,Arrays,Plist,Nsmutabledictionary,我有一个UITableView,它由NSMutableDictionary中的数组键填充 为了能够删除这些数组,我需要能够以某种方式获取密钥。我的想法最简单的是从这一行的标签上得到它 我正在使用以下代码加载字典: NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); // get documents path NSString *documentsPat
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:];
要从字典中删除数组,请执行以下操作:
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:];
但很明显,我没有找到它们的关键点,因为我不知道如何通过编程抓住它们。有什么建议吗
干杯
克里斯
删除代码:
- (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];
}
从不从表格单元格获取数据。您已经有了用于填充表格单元格的数据结构,可以从这些数据结构中获取数据 您必须有某种数组,以便为每个单元格获取适当的数据。使用相同的数组获取要删除的行的数据 如果为
cellforrowatinexpath:
方法显示一些代码,则可以给出更具体的答案。但基于indepath
获取数据的代码基本相同
到目前为止,您发布的代码只显示字典。字典不是基于行的结构(如表)的良好基础
更新:根据单元格中的代码,您只需要:
NSString *key = [viewerKeys objectAtIndex:indexPath.row];
更新2:为什么要用committeeditingstyle
方法再次加载文件?您需要修改已为表加载的现有数据结构。现在,表使用了数据结构。然后在committeditingstyle
中,再次加载数据,从临时数据中删除一个条目,然后告诉表重新加载。重新加载将使用原始数据结构,而不是此临时数据
另外,不要同时调用reloadData
和deleteRowsAtIndexPaths:
。打个电话就行了
这一行:
[dictionary removeObjectForKey:[NSString stringWithFormat:@"%@", key]];
应该是:
[dictionary removeObjectForKey:key];
不要使用stringWithFormat
,除非您确实有一个正在格式化的字符串。永远不要从表格单元格中获取数据。您已经有了用于填充表格单元格的数据结构,可以从这些数据结构中获取数据
您必须有某种数组,以便为每个单元格获取适当的数据。使用相同的数组获取要删除的行的数据
如果为cellforrowatinexpath:
方法显示一些代码,则可以给出更具体的答案。但基于indepath
获取数据的代码基本相同
到目前为止,您发布的代码只显示字典。字典不是基于行的结构(如表)的良好基础
更新:根据单元格中的代码,您只需要:
NSString *key = [viewerKeys objectAtIndex:indexPath.row];
更新2:为什么要用committeeditingstyle
方法再次加载文件?您需要修改已为表加载的现有数据结构。现在,表使用了数据结构。然后在committeditingstyle
中,再次加载数据,从临时数据中删除一个条目,然后告诉表重新加载。重新加载将使用原始数据结构,而不是此临时数据
另外,不要同时调用reloadData
和deleteRowsAtIndexPaths:
。打个电话就行了
这一行:
[dictionary removeObjectForKey:[NSString stringWithFormat:@"%@", key]];
应该是:
[dictionary removeObjectForKey:key];
不要使用stringWithFormat
,除非您实际有一个正在格式化的字符串。[[dictionary allKeys]objectAtIndex:indexPath.row]
显示plist的几行。(在Xcode中,右键单击并“打开为”->“源代码”)。请不要将此作为注释放入您的问题中。@H2CO3我认为这不能保证正确的顺序。…[[dictionary allKeys]objectAtIndex:indexath.row]
显示您的plist的几行。(在Xcode中,右键单击并“打开为”->“源代码”)。请不要将此作为注释放入您的问题中。@H2CO3我认为这不能保证正确的顺序。…-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@“ViewerName”];UILabel*label=(UILabel*)[cell viewWithTag:1000];label.text=[viewerKeys objectAtIndex:indexath.row];返回单元格;}
Ok,myviewerKeys
是一个ivarNSArray
,它保存plist中的所有键。当我在viewerKeys=[dictionary allKeys]
中将其更改为NSMutableArray*.\uuuu-strong时,我从'NSArray*.
中得到了分配给'NSMutableArray*.\uu-strong'的不兼容指针类型的错误。。。我确信我在这里犯了一些明显的错误……好吧,我已经成功地将viewerKeys更改为[viewerKeys addObjectsFromArray:[dictionary allKeys]]
但是现在我的表没有被填充…-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“ViewerName”];UILabel*label=(UILabel*)[cell viewWithTag:1000];label.text=[viewerKeys objectAtIndex:indexath.row];返回单元格;}
Ok,myviewerKeys
是一个ivarNSArray
,它保存plist中的所有键。当我在viewerKeys=[dictionary allKeys]
中将其更改为NSMutableArray*.\uuuu-strong时,我从'NSArray*.
中得到了分配给'NSMutableArray*.\uu-strong'的不兼容指针类型的错误。。。我确信我在这里犯了一些明显的错误……好吧,我已经成功地将viewerKeys更改为[viewerKeys addObjectsFromArray:[dictionary allKeys]]代码>但是现在我的表没有被填充。。。