內部字典這是我的JSON響應如何更換一個NSMutableArray
data = {
Apparels = (
{
"category_id" = 1650002;
"category_name" = Apparels;
"country_id" = 1;
"created_by" = 1;
"created_on" = "2016-03-17 13:29:00";
id = 2;
"is_checked" = 0;
"is_delete" = 1;
"modified_by" = 1;
"modified_on" = "2016-08-26 05:53:21";
"short_name" = UP;
status = 550002;
value = UP;
},
{
"category_id" = 1650002;
"category_name" = Apparels;
"country_id" = 1;
"created_by" = 1;
"created_on" = "2016-03-17 13:29:00";
id = 3;
"is_checked" = 0;
"is_delete" = 1;
"modified_by" = 1;
"modified_on" = "2016-08-26 10:41:12";
"short_name" = "<null>";
status = 550001;
value = MP;
},
{
"category_id" = 1650002;
"category_name" = Apparels;
"country_id" = 1;
"created_by" = 4;
"created_on" = "2016-04-08 12:10:17";
id = 4;
"is_checked" = 0;
"is_delete" = 1;
"modified_by" = 1;
"modified_on" = "2017-03-06 10:35:04";
"short_name" = HR;
status = 550002;
value = Haryana;
},
{
"category_id" = 1650002;
"category_name" = Apparels;
"country_id" = 1;
"created_by" = 4;
"created_on" = "2016-04-08 12:10:36";
id = 5;
"is_checked" = 0;
"is_delete" = 1;
"modified_by" = 1;
"modified_on" = "2016-08-26 05:53:29";
"short_name" = HP;
status = 550002;
value = "Himachal Pardesh";
}
);
};
到目前爲止,我可以這樣造屏。我希望該用戶可以檢查和取消選中產品。爲此,我需要將「is_checked」的值從「0」替換爲「1」。這是我的代碼。
detailArr = [responseDict[@"data"] mutableCopy]; //MutableArray
keys = [responseDict[@"data"] allKeys];//MutableArray
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return detailArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section{
NSDictionary *objectDict = [detailArr valueForKey:[keys objectAtIndex:section]];
return objectDict.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(tableView.frame.origin.x,0, self.view.frame.size.width, 40)];
UILabel *labelVw = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 275, 35)];
labelVw.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
labelVw.text = [keys objectAtIndex:section];
headerView.backgroundColor = [UIColor whiteColor];
[headerView addSubview:labelVw];
tableView.tableHeaderView = headerView;
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 5;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
if(![[[[detailArr valueForKey:[keys objectAtIndex:indexPath.section]] valueForKey:@"is_checked"] objectAtIndex:indexPath.row] isEqual:@"1"])
{
cell.titleLbl.text = [[[detailArr valueForKey:[keys objectAtIndex:indexPath.section]] valueForKey:@"value"] objectAtIndex:indexPath.row];
[cell.checkBtn setImage:[UIImage imageNamed:@"Unchecked Checkbox.png"] forState:UIControlStateNormal];
[cell.bookmarkBtn setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
}else{
[cell.checkBtn setImage:[UIImage imageNamed:@"Checked Checkbox.png"] forState:UIControlStateNormal];
[cell.bookmarkBtn setImage:[UIImage imageNamed:@"star_yellow.png"] forState:UIControlStateNormal];
cell.titleLbl.attributedText = [ValidationClass LineOnLabel:[[detailArr valueForKey:@"value"] objectAtIndex:indexPath.row]];
}
[cell.checkBtn addTarget:self action:@selector(actionCheck:) forControlEvents:UIControlEventTouchUpInside];
cell.layer.borderWidth = 1.0f;
cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
return cell;
}
- (void)actionCheck:(UIButton*)sender{
UIButton *btn = (UIButton*)sender;
tagvalue = [ValidationClass getRowByBtn:btn :_tblVw]; //returns indexPath.row by click of button inside cell
sectionValue = [ValidationClass getSectionByBtn:btn :_tblVw]; //returns section of tableview
NSMutableDictionary *replaceDict = [[NSMutableDictionary alloc]init];
if([[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] valueForKey:@"is_checked"] objectAtIndex:tagvalue] isEqual:@"1"]){
[replaceDict setObject:[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] valueForKey:@"value"] objectAtIndex:tagvalue]forKey:@"category_id"];
[replaceDict setObject:[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] valueForKey:@"value"] objectAtIndex:tagvalue] forKey:@"value"];
[replaceDict setObject:@"0" forKey:@"is_checked"];
[detailArr replaceObjectAtIndex:tagvalue withObject:replaceDict];
[_tblVw reloadData];
[self addProduct:detailArr andPosition:tagvalue];
}else{
[replaceDict setObject:[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] valueForKey:@"value"] objectAtIndex:tagvalue]forKey:@"category_id"];
[replaceDict setObject:[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] valueForKey:@"value"] objectAtIndex:tagvalue] forKey:@"value"];
[replaceDict setObject:@"1" forKey:@"is_checked"];
NSMutableArray *arr = [[detailArr valueForKey:[keys objectAtIndex:sectionValue]] mutableCopy];
[arr replaceObjectAtIndex:tagvalue withObject:replaceDict];
detailArr = [arr mutableCopy]; // this removes previous data and add this. But I want to overwrite the existing data with replace dict at particular index.
NSLog(@"%@",detailArr);
[_tblVw reloadData];
[self addProduct:detailArr andPosition:tagvalue];
}
NSLog(@"%@", detailArr);
}
的NSLog返回此:
(
{
"category_id" = UP;
"is_checked" = 1;
value = UP;
},
{
"category_id" = 1650002;
"category_name" = Apparels;
"country_id" = 1;
"created_by" = 1;
"created_on" = "2016-03-17 13:29:00";
id = 3;
"is_checked" = 0;
"is_delete" = 1;
"modified_by" = 1;
"modified_on" = "2016-08-26 10:41:12";
"short_name" = "<null>";
status = 550001;
value = MP;
},
{
"category_id" = 1650002;
"category_name" = Apparels;
"country_id" = 1;
"created_by" = 4;
"created_on" = "2016-04-08 12:10:17";
id = 4;
"is_checked" = 0;
"is_delete" = 1;
"modified_by" = 1;
"modified_on" = "2017-03-06 10:35:04";
"short_name" = HR;
status = 550002;
value = Haryana;
},
{
"category_id" = 1650002;
"category_name" = Apparels;
"country_id" = 1;
"created_by" = 4;
"created_on" = "2016-04-08 12:10:36";
id = 5;
"is_checked" = 0;
"is_delete" = 1;
"modified_by" = 1;
"modified_on" = "2016-08-26 05:53:29";
"short_name" = HP;
status = 550002;
value = "Himachal Pardesh";
}
)
在此之後,它崩潰的numberOfRowsInSection。
任何幫助,將不勝感激。提前致謝。
Post crash log。 – kb920