gpt4 book ai didi

ios - 如何从 NSmutableDictionary 中删除值(不是键)

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:14:43 26 4
gpt4 key购买 nike

我有一个 NSmutableDictionary,我用它来填充 tableView。现在我想让我的 tableView 中的行可编辑。这是我的代码:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;

}

然后当一个人选择行

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
//Below four lines to get the name of course to delete

NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];//This is the key
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];//This is the value

[_sectionContents removeObjectForKey:key];//this can delete the key,not fit here, should delete value
NSLog(@"Dictionary: %@", [_sectionContents description]);//test dictionary here
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}

在这里,我的情况是一个键可能有多个值,所以当用户选择要删除的行时,上面的代码将删除具有其他值的整个键,这不是我想要的,它也会产生错误。

所以我的问题是不使用 removeObjectForKey,请问从字典中只删除值而不是键的正确方法是什么?

这是我填充字典的代码:

//New code for populating dictionary
if ([_sectionContents objectForKey:AddKey] != nil) {
//Already exist a value for the key
id object = [_sectionContents objectForKey:AddKey];
NSMutableArray *objectArray;
//objectArray = [[NSMutableArray alloc] init];
if ([object isKindOfClass:[NSMutableArray class]]) {//if object is a mutableArray already

objectArray = (NSMutableArray *)object;
//NSLog(@"1.The code runs through here!");
} else {//If object is not array, it means that only one value exists here.

//The following convert string to correct format
NSString *oldCourse=nil;

if ([object isKindOfClass:[NSString class]]) {
oldCourse = object;
} else if ([object respondsToSelector:@selector(stringValue)]) {
oldCourse = [object stringValue];
} else if ([object respondsToSelector:@selector(description)]) {
oldCourse = [object description];
}

NSString *newString = [[oldCourse componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@" "];//remove newline
NSString * newString2 = [newString substringWithRange:NSMakeRange(7, [newString length]-7)];//cut the front
//To erase the symbols
NSString *newString3 = [newString2 stringByReplacingOccurrencesOfString:@")" withString:@""];
NSString *newString4 = [newString3 stringByReplacingOccurrencesOfString:@"\"" withString:@""];

//NSLog(@"%@",newString4);
//if ([oldCourse isKindOfClass:[NSString class]])
//NSLog(@"Become string here!");

objectArray = [[NSMutableArray alloc] init];
if(![objectArray containsObject:newString4])
if(![_msgCourse containsObject:newString4])
[objectArray insertObject:newString4 atIndex:0];
// NSLog(@"2.The code runs through here!");
//NSMutableArray *objectArray = [[NSMutableArray alloc] init];
}
NSLog(@"%@",objectArray);

NSString *message;
if([objectArray containsObject:course])//Giving out warning if course retaken
{UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error: this course have been selected already." message:message delegate:nil cancelButtonTitle:@"Return to course planner" otherButtonTitles:nil, nil];

[alert show];}

if(![objectArray containsObject:course] && ![course isEqualToString:[objectArray objectAtIndex:0]] &&
![_msgCourse containsObject:course])
[objectArray addObject:course];

if(![_msgCourse containsObject:course])//Use this flag to avoid retake class
[_msgCourse addObject:course];

//NSLog(@"3.The code runs through here!");

[_sectionContents setObject:objectArray forKey:AddKey];
} else {
//No value for the key, add new value
NSString *message;
if([_msgCourse containsObject:course])//If course existed
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error: this course have been selected already." message:message delegate:nil cancelButtonTitle:@"Return to course planner" otherButtonTitles:nil, nil];

[alert show];
}

if(![_msgCourse containsObject:course])
{
[_sectionContents setObject:[NSArray arrayWithObjects:course,nil] forKey:AddKey];
[_msgCourse addObject:course];
}

}

最佳答案

如果我没理解错的话,你试图从部分中删除元素,但实际上删除了整个部分。接下来你应该做的是:

NSString *key = [_sectionKeys objectAtIndex:[indexPath section]];//This is the key
NSMutableArray *contents = _sectionContents[key];
//Delete only selected row
[contents removeObjectAtIndex:indexPath.row]

这应该有效。

关于ios - 如何从 NSmutableDictionary 中删除值(不是键),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30104778/

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