-1

我有一個NSMutableArray,它構成了包含或保存NSMutableDictionary的NSMutableArray。替換NSMutableDicitionary中的值,它位於NSMutableArray內部

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
self.userNameArray = [NSMutableArray arrayWithArray:[userDefaults arrayForKey:@"userNameTextArray"]]; 

NSMutableArray *arrayToAdd = [[NSMutableArray alloc] init]; 
for (id object in self.userNameArray) { 
    [arrayToAdd addObject:@"Negative"]; 
} 

self.namesDictionary = [@{ @"userNameText" : self.categoriesMutableNameArray, @"selectedCellState" : arrayToAdd}mutableCopy]; 

self.namesFinalArr = [[NSMutableArray alloc] init]; 
self.namesDictMutableDict = [NSMutableDictionary dictionaryWithDictionary:self.namesDictionary]; 
[self.namesFinalArr addObject:self.namesDictMutableDict]; 

結果在我上面的代碼NSLog的是這樣的:

(
    { 
    selectedCellState =   (
     Negative, 
     Negative, 
     Negative, 
     Negative, 
     Negative, 
     Negative, 
     Negative, 
     Negative, 
     Negative, 
     Negative, 
     Negative, 
     Negative, 
     Negative, 
     Negative 
    ); 
    userNameText =   (
     "userText - Text", 
     "userText1 - Text1", 
     "userText2 - Text2", 
     "userText3 - Text3", 
     "userText4 - Text4", 
     "userText5 - Text5", 
     "userText6 - Text6", 
     "userText7 - Text7", 
     "userText8 - Text8", 
     "userText9 - Text9", 
     "userText10 - Text10", 
     "userText11 - Text11", 
     "userText12 - Text12", 
     "userText13 - Text13" 
    ); 
} 
) 

我使用一個UITableView和我填充self.namesFinalArr的UITableView的。在這樣的CellForRow方法和它的工作原理:

labelForTableCell.text = [[[self.namesFinalArr objectAtIndex:0] objectForKey:@"userNameText"]objectAtIndex:indexPath.row]; 

這種填充我與userNameText下的數據的UITableView在self.namesFinalArr

我試圖啓用或細胞禁用圖像時被選中,我把它使用didDeselectRowAtIndexPath和didSelectRowAtIndexPath方法的方法來顯示和隱藏UImageview

這工作,但我想更新self.namesFinalArr在被按下的索引列或行selectedCellState但我得到下面的錯誤。

didselectRowAtIndexPath方法我做這樣的事情:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     self.selectedRows = [self.tableView indexPathsForSelectedRows]; 

     [[[[self.namesFinalArr objectAtIndex:0] objectForKey:@"selectedCellState"]objectAtIndex:indexPath.row] setValue:@"Positive" forKey:@"selectedCellState"]; 

} 

當試圖改變數組值和索引行我得到一個錯誤:

 Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFConstantString 0x23fef0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key selectedCellState.' 
*** First throw call stack: 
    (
0 CoreFoundation      0x020c75e4 __exceptionPreprocess + 180 
1 libobjc.A.dylib      0x01e4a8b6 objc_exception_throw + 44 
2 CoreFoundation      0x021576a1 -[NSException raise] + 17 
3 Foundation       0x0075d9ee -[NSObject(NSKeyValueCoding) setValue:forUndefinedKey:] + 282 
4 Foundation       0x006c9cfb _NSSetUsingKeyValueSetter + 88 
5 Foundation       0x006c9253 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 267 
6 Piccing        0x0004880c -[PiccImageCategoriesViewController tableView:didSelectRowAtIndexPath:] + 828 
7 UIKit        0x010a77b1 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1513 
8 UIKit        0x010a7924 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 279 
9 UIKit        0x010ab908 __38-[UITableView touchesEnded:withEvent:]_block_invoke + 43 
10 UIKit        0x00fe2183 ___afterCACommitHandler_block_invoke + 15 
11 UIKit        0x00fe212e _applyBlockToCFArrayCopiedToStack + 403 
12 UIKit        0x00fe1f5a _afterCACommitHandler + 532 
13 CoreFoundation      0x0208f4ce __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30 
14 CoreFoundation      0x0208f41f __CFRunLoopDoObservers + 399 
15 CoreFoundation      0x0206d344 __CFRunLoopRun + 1076 
16 CoreFoundation      0x0206cac3 CFRunLoopRunSpecific + 467 
17 CoreFoundation      0x0206c8db CFRunLoopRunInMode + 123 
18 GraphicsServices     0x027779e2 GSEventRunModal + 192 
19 GraphicsServices     0x02777809 GSEventRun + 104 
20 UIKit        0x00fc5d3b UIApplicationMain + 1225 
21 Piccing        0x000138cd main + 141 
22 libdyld.dylib      0x02f4d70d start + 1 
23 ???         0x00000001 0x0 + 1 

) 的libC++ ABI。 dylib:以NSException類型的未捕獲異常終止 (lldb)

+0

此Log是哪個數組? – preetam

+0

self.namesFinalArr – user3110353

回答

1

在這種情況下,有一個數組包含兩個字典中的每一個都包含數組,並且您想要更改數組中的值,並且應該使用此方法更改值

[[[self.namesFinalArr objectAtIndex:0] objectForKey:@"selectedCellState"] replaceObjectAtIndex:indexPath.row withObject:@"Positive"];