2011-08-27 5 views
2

나는 단면 표에 사용중인이 NSMutableArray에서 항목 하나를 삭제하는 방법을 찾으려고합니다. 데이터 소스를 만드는 방법은 다음과 같습니다.특정 키에 대해 NSMutableArray에서 항목을 삭제하십시오.

listOfItems = [[NSMutableArray alloc] init]; 

NSArray *appleComputers = [NSArray arrayWithObjects:@"iPhone",@"iPod",@"MacBook",@"MacBook Pro",nil]; 
NSDictionary *appleComputersDict = [NSDictionary dictionaryWithObject:appleComputers forKey:@"Computers"]; 

NSArray *otherComputers = [NSArray arrayWithObjects:@"HP", @"Dell", @"Windows", nil]; 
NSDictionary *otherComputersDict = [NSDictionary dictionaryWithObject:otherComputers forKey:@"Computers"]; 

[listOfItems addObject:appleComputersDict]; 
[listOfItems addObject:otherComputersDict]; 

이 배열에서 "iPod"이라고 말하면 어떻게해야합니까? 나는 그들 가운데 여러 가지를 시도했지만 아래는 하나도 효과가 없었다.

[listOfItems removeObjectAtIndex:indexPath.row]; 

감사합니다 (여기에 문제가 ... 실제 부분에 대한 언급이 없다는 것입니다)!

UPDATE :

- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

if (editingStyle == UITableViewCellEditingStyleDelete) { 


    NSMutableArray * section = [listOfItems objectAtIndex:indexPath.section];  
    [section removeObjectAtIndex:indexPath.row]; 
[tblSimpleTable reloadData]; 

} else if (editingStyle == UITableViewCellEditingStyleInsert) { 

    // some code... 
} 

가}

답변

2

왜 사전을 사용하고 있는지 확실하지 않습니다. 필요하지 않은 것 같습니다. 어레이의 배열은 어떨까요? 당신이 사전을 사용하는 필요성을 하더라도 다음

NSMutableArray * section = [listOfItems objectAtIndex:indexPath.section];  
[section removeObjectAtIndex:indexPath.row]; 

listOfItems = [[NSMutableArray alloc] init]; 

NSMutableArray * appleComputers = [NSMutableArray arrayWithObjects:@"iPhone",@"iPod",@"MacBook",@"MacBook Pro",nil]; 
[listOfItems addObject:appleComputers]; 

NSMutableArray * otherComputers = [NSMutableArray arrayWithObjects:@"HP", @"Dell", @"Windows", nil]; 
[listOfItems addObject:otherComputers]; 

, 그것은 당신의 appleComputers 또는 otherComputers에 대한 NSMutableArray 대신 NSArray를 사용하여이 작업을 수행 할 ... 여전히 큰 문제가 아니다 :

NSDictionary * section = [listOfItems objectAtIndex:indexPath.section]; 
NSMutableArray * sectionComputers = [section objectForKey:@"Computers"];  
[sectionComputers removeObjectAtIndex:indexPath.row]; 
+0

좋은 시작이지만,이 솔루션은 원본 데이터 소스를 업데이트하지 않고 삭제 된 항목이없는 새 배열 만 생성합니다. – TommyG

+0

새로운 배열을 전혀 만들지 않습니다. 처음에 목록에 추가 한 배열을 업데이트합니다. 정적 항목이있는 샘플 코드에서 설명한 것과 다른 "원본 데이터 원본"이있는 경우 질문을 분명히해야합니다. – Steve

+0

나는 그렇지 않습니다. listOfItems만이 유일합니다. 그러나 코드에서 listOfItems에서 "section"을 만든 다음 listOfItems의 하위 집합 인 "section"에서 객체를 제거하고 더 이상 그 객체의 일부가 아닌 객체를 제거합니다. 그래서 listOfItems는 업데이트되지 않습니다. – TommyG

0

당신이 [: @ "아이팟"배열 removeObject]를 봤어 : 내가 행을 삭제하는 곳은?

+0

"iPod"이 여러 개있는 경우 어떻게해야합니까? 참조를 위해 indexpath 속성을 사용해야 할 것입니다. 그래도 고마워. – TommyG

1

예를 들어 'iPod'항목을 모두 삭제하면 이 (변경 가능한 모음으로 변경) :

listOfItems = [[NSMutableArray alloc] init]; 

NSMutableArray *appleComputers = [NSMutableArray arrayWithObjects:@"iPhone",@"iPod",@"MacBook",@"MacBook Pro",nil]; 
NSMutableDictionary *appleComputersDict = [NSMutableDictionary dictionaryWithObject:appleComputers forKey:@"Computers"]; 

NSMutableArray *otherComputers = [NSMutableArray arrayWithObjects:@"HP", @"Dell", @"Windows", nil]; 
NSMutableDictionary *otherComputersDict = [NSMutableDictionary dictionaryWithObject:otherComputers forKey:@"Computers"]; 

[listOfItems addObject:appleComputersDict]; 
[listOfItems addObject:otherComputersDict]; 

... 

for (NSMutableDictionary *dict in listOfItems) { 
    [dict objectForKey:@"Computers" removeObject:@"iPod"]; 
} 
관련 문제