如何在Objective C中的didFinishLoading中获取IndexPath.row

时间:2016-11-28 06:48:29

标签: ios objective-c xcode nsarray nsdictionary

我是iOS的新手,我遇到了关于在cellforrowAtIndexPath方法之外获取IndexPath.row的问题,即在didFinishLoading上。我的代码就像这样

 - (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);
}

我只得到一个值为0的值。

提前致谢!

3 个答案:

答案 0 :(得分:1)

只需为数组添加for循环:

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];

}

在您的情况下,您可以像这样使用它:

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);

  }

答案 1 :(得分:1)

NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];

有关详细信息,请访问:https://developer.apple.com/reference/uikit/uitableview/1615000-indexpathforselectedrow

希望这有帮助。

答案 2 :(得分:0)

您可以使用以下代码获取索引路径,并且您可以从此索引路径获取所选单元格。

let indexPath = self.tblObj.indexPathForSelectedRow!

let currentCell = self.tblObj.cellForRowAtIndexPath(indexPath)! as! YourTableViewCell