Nivesh Singh Nivesh Singh - 23 days ago 6
iOS Question

json data populate and multiselection to get Ids

I have to post a JSON and in response get user Id and many more things.I have added it to an array and populate my tableview. like this.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ModelClass * model = __arrayForContacts[indexPath.row];

MYcustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
}


now, I have applied multi-selection to my tableview.I want to get the user id from multi-selected row.I want it to post as request in next JSON parsing.How can i achieve this, any help would be thankful.

Answer

In tableview's didselect method you need to set as below

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    yourobjectClass *objCookBook = [arrCookBook objectAtIndex:indexPath.row];
    objCookBook.isSelected = !objCookBook.isSelected;
    //[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    //[tableView deselectRowAtIndexPath:indexPath animated:YES];// uncomment above line if you want to reload table
}

You can get as below :

// isSelected is your flag for selection

     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isSelected == 1"];
      NSArray *arrSelected = [arrCookBook filteredArrayUsingPredicate:predicate];
     if (arrSelected.count > 0) {
       NSlog(@"%@",[arrSelected valueForKey:@"userId"]); // replace your id name and post it to json 

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[arrSelected valueForKey:@"userId"] options:NSJSONWritingPrettyPrinted error:nil];
    NSString *JsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
    }