我有一组来自 JSON 提要的字典。这填充了一个UITableView
. 当用户选择一行时,我使用UITableViewCellAccessoryCheckmark
,并且我正在尝试修改该行的数组内的字典,因此当滚动后再次重用单元格时,它保持正确。但是我遇到了一个错误。这是我的代码(为清楚起见而缩短):
JSON数据方式:
+ (NSMutableArray *)parseKennelData:(NSString *)type userID:(NSString *)user dogID:(NSString *)dog terms:(NSString *)terms
{
NSURL *searchURL=[NSURL URLWithString:searchLocation];
// download and parse the JSON
NSData *JSONData = [[[NSData alloc] initWithContentsOfURL:searchURL] autorelease ];
NSError *error=nil;
CJSONDeserializer *deserializer = [CJSONDeserializer deserializer];
NSMutableDictionary *JSONdictionary =[deserializer deserializeAsDictionary:JSONData error:&error];
NSMutableArray *allResults = [JSONdictionary valueForKey:@"kennel"];
return allResults;
}
视图控制器:
interface
{
NSMutableArray *results;
}
- (void)viewDidLoad
{
// data for search
self.results=[JSONData parseKennelData:@"search" userID:@"demo" dogID:@"" terms:self.terms];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *rowData = [self.results objectAtIndex:[indexPath row]];
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if (thisCell.accessoryType == UITableViewCellAccessoryNone)
{
thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
thisCell.accessoryView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"tick-button.png"]];
[rowData setObject:@"demo" forKey:@"user_id" ];
}
else
{
thisCell.accessoryType = UITableViewCellAccessoryNone;
thisCell.accessoryView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"plusbutton.png"]];
[rowData setObject:@"" forKey:@"user_id"];
}
[self.results replaceObjectAtIndex:[indexPath row] withObject:rowData];
}
我收到 SIGABRT 错误:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
我以为我已经确保我所有的东西都是可变的,所以我看不到它在哪里倒下。
谢谢