Frage

I have an NSMutableArray that I have build up that consists or holds a 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];

The result in my NSlog of the above code is like this:

(
    {
    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"
    );
}
) 

I am using a UITableview and I populate the UITableview with self.namesFinalArr . in the CellForRow method like this and it works:

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

This populates my UITableview with the data under userNameText in self.namesFinalArr

I am trying to enable or disable an image on a cell when it is selected and I use the didDeselectRowAtIndexPath and didselectRowAtIndexPath methods to show and hide a UImageview

This works but I am trying to update selectedCellState in self.namesFinalArr at the index row or row that was pressed but I get the following error.

In the didselectRowAtIndexPath method I do something like this:

 - (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"];

}

When trying to change the array value and index row I get a error:

     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: terminating with uncaught exception of type NSException (lldb)

War es hilfreich?

Lösung

In this case,there is one array which contains two dictionary each contains array and you are suppose to change the values in array and you should used this method to change the value

[[[self.namesFinalArr objectAtIndex:0] objectForKey:@"selectedCellState"] replaceObjectAtIndex:indexPath.row withObject:@"Positive"];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top