2017-03-22 49 views
0

內部字典這是我的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"; 
     } 
    ); 
}; 

enter image description here

到目前爲止,我可以這樣造屏。我希望該用戶可以檢查和取消選中產品。爲此,我需要將「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。

任何幫助,將不勝感激。提前致謝。

+1

Post crash log。 – kb920

回答

0

這段代碼的結果是的NSDictionary,但你創建一個數組對象: responseDict[@"data"];

0

您可以與服務器商量,數據必須是一個集合,該NSArray的格式,現在它是一個的NSDictionary

0

你有一些錯誤,首先解決這個問題,這個錯誤是在

  1. 您在複選框按鈕設置標籤單元設計。

    [cell.checkBtn setTag:indexPath.row];

  2. 管理檢測複選框按鈕的龍頭作用。

    UIButton *button = (UIButton *)sender; int row = button.tag;

  3. 使用該行對象來管理替換對象。

    A.首先你得到選定的對象並存儲在一個NSMutableDictionary對象中。

    NSMutableDictionary *dic = [detailArr objectAtIndex:row];

    B.更換對象作爲設定值。

    [dic setValue:@"1" forKey:@"is_checked"];

    C.該替換值的變化僅 - 「DIC」對象,而不是所選細胞,所以下面的列表是在你的數組列表變化對象。

    [detailArr replaceObjectAtIndex:row withObject:dic];

    D.和最後你有實現代碼如下reload the selected cell

0

問題是因爲「Apparels」內的數組是不可變的。我終於找到了解決方案。

detailArr = [[NSMutableArray alloc]init]; 
    if([responseDict[@"data"] count] > 0){ 
     keys = [responseDict[@"data"] allKeys]; 
     NSMutableDictionary *dict1; 
     NSMutableArray *arrx; 
     for(int i=0;i<[responseDict[@"data"] count];i++){ 
      arrx=[[NSMutableArray alloc]init]; 
      arrx = [NSMutableArray arrayWithArray:[responseDict[@"data"] valueForKey:[keys objectAtIndex:i] ]]; 
      dict1=[[NSMutableDictionary alloc]init]; 
      [dict1 setObject:arrx forKey:[keys objectAtIndex:i]]; 
      [detailArr addObject:dict1]; 
      } 

    } 
    [_tblVw reloadData]; 

代碼複選框功能通過在特定索引處替換JSON響應數組中的字典。

- (void)actionCheck:(UIButton*)sender{ 
     UIButton *btn = (UIButton*)sender; 
     tagvalue = [ValidationClass getRowByBtn:btn :_tblVw]; 
     sectionValue = [ValidationClass getSectionByBtn:btn :_tblVw]; 

     NSMutableDictionary *replaceDict = [[NSMutableDictionary alloc]init]; 
     if([[[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] objectAtIndex:sectionValue] valueForKey:@"is_checked"] objectAtIndex:tagvalue] isEqual:@"1"]){ 
     [replaceDict setObject:[[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] objectAtIndex:sectionValue] valueForKey:@"category_id"] objectAtIndex:tagvalue]forKey:@"category_id"]; 
     [replaceDict setObject:[[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] objectAtIndex:sectionValue] valueForKey:@"id"] objectAtIndex:tagvalue]forKey:@"id"]; 
     [replaceDict setObject:[[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] objectAtIndex:sectionValue] valueForKey:@"value"] objectAtIndex:tagvalue] forKey:@"value"]; 
     [replaceDict setObject:@"0" forKey:@"is_checked"]; 
     [[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] objectAtIndex:sectionValue] replaceObjectAtIndex:tagvalue withObject:replaceDict]; 
     NSLog(@"%@",detailArr); 

     [_tblVw reloadData]; 
     [self addProduct:detailArr andPosition:tagvalue]; 
    }else{ 
     [replaceDict setObject:[[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] objectAtIndex:sectionValue] valueForKey:@"category_id"] objectAtIndex:tagvalue]forKey:@"category_id"]; 
     [replaceDict setObject:[[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] objectAtIndex:sectionValue] valueForKey:@"id"] objectAtIndex:tagvalue]forKey:@"id"]; 
     [replaceDict setObject:[[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] objectAtIndex:sectionValue] valueForKey:@"value"] objectAtIndex:tagvalue] forKey:@"value"]; 

     [replaceDict setObject:@"1" forKey:@"is_checked"]; 
     [[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] objectAtIndex:sectionValue] replaceObjectAtIndex:tagvalue withObject:replaceDict]; 

     NSLog(@"%@",detailArr); 
     [self addProduct:detailArr andPosition:tagvalue]; 
    } 

     NSLog(@"%@", detailArr); 

} 
相關問題