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

how to get IndexPath.row in didFinishLoading in Objective C

我是 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 的值。

提前致谢!

只需为数组添加一个 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);

  }

NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];

更多详情如下:https://developer.apple.com/reference/uikit/uitableview/1615000-indexpathforselectedrow

希望对您有所帮助。

您可以使用下面的代码获取索引路径,您还可以从该索引路径获取选定的单元格。

let indexPath = self.tblObj.indexPathForSelectedRow!

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