Muju Muju - 12 days ago 8
Objective-C Question

how to get IndexPath.row in didFinishLoading in Objective C

I am new in iOS and I am facing problem regarding to get IndexPath.row outside cellforrowAtIndexPath method i.e on didFinishLoading. My code is like this

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
loginStatus = [[NSString alloc] initWithBytes: [myNSMDataFromServer mutableBytes] length:[myNSMDataFromServer length] encoding:NSUTF8StringEncoding];
NSLog(@"loginStatus =%@",loginStatus);
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:loginStatus error:&parseError];
NSLog(@"JSON DICTIONARY = %@",xmlDictionary);
recordResult = [xmlDictionary[@"success"] integerValue];
NSLog(@"Success: %ld",(long)recordResult);
NSDictionary* Address=[xmlDictionary objectForKey:@"soap:Envelope"];
NSLog(@"Address Dict = %@",Address);
NSDictionary *new =[Address objectForKey:@"soap:Body"];
NSLog(@"NEW DICT =%@",new);
NSDictionary *LoginResponse=[new objectForKey:@"TFMComplaints_GetNewResponse"];
NSLog(@"Login Response DICT =%@",LoginResponse);
NSDictionary *LoginResult=[LoginResponse objectForKey:@"TFMComplaints_GetNewResult"];
NSLog(@"Login Result =%@",LoginResult);
if(LoginResult.count>0)
{
NSLog(@"Login Result = %@",LoginResult);
NSLog(@"Login Result Dict =%@",LoginResult);
NSString *teststr =[[NSString alloc] init];
teststr =[LoginResult objectForKey:@"text"];
NSLog(@"Test String Value =%@",teststr);
NSString *string = [LoginResult valueForKey:@"text"];
NSLog(@"Now String is =%@",string);
NSData *data =[string dataUsingEncoding:NSUTF8StringEncoding];
NSError* error;
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSUTF8StringEncoding error:&error];
NSIndexPath *selectedIndexPath = [closetbl indexPathForSelectedRow];
NSDictionary *firstObj = [array objectAtIndex:selectedIndexPath.row];

idarray=[[NSMutableArray alloc]init];

idarray=[firstObj valueForKey:@"Key"];
NSLog(@"Result Array =%@",idarray);
}


I am getting only one value that is on 0.

Thanks in Advance!

Answer

Just add a for loop for array:

for (int i = 0; i < array.count; i++)
{
    //where i will be the index path. You can use it like this 
    NSDictionary *firstObj = [array objectAtIndex:i];

}

In your case you can use it like this:

idarray=[[NSMutableArray alloc]init];

for (int i = 0; i < array.count; i++){
    NSDictionary *firstObj = [array objectAtIndex:i];
    [idarray addObject:[firstObj valueForKey:@"Key"]];
    NSLog(@"Result Array =%@",idarray);

  }