替换位于NSMutableArray内的NSMutableDicitionary中的值

时间:2014-02-28 07:13:28

标签: ios objective-c uitableview nsmutablearray nsmutabledictionary

我有一个构建的NSMutableArray,它包含或包含NSMutableDictionary。

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

中的self.namesFinalArr下的数据填充我的UITableview

我正在尝试在单元格选中时启用或禁用单元格,我使用didDeselectRowAtIndexPath和didselectRowAtIndexPath方法来显示和隐藏UImageview

这样可行但我正在尝试更新selectedCellState中已按下的索引行或行的self.namesFinalArr但我收到以下错误。

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)

1 个答案:

答案 0 :(得分:1)

在这种情况下,有一个数组包含两个字典,每个字典包含数组,您可以更改数组中的值,您应该使用此方法更改值

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