문제 설명
Objective C의 didFinishLoading에서 IndexPath.row를 얻는 방법 (how to get IndexPath.row in didFinishLoading in Objective C)
저는 iOS를 처음 사용하고 있으며, didFinishLoading에서 cellforrowAtIndexPath 메서드 외부에서 IndexPath.row를 가져오는 것과 관련된 문제에 직면하고 있습니다. 내 코드는 다음과 같습니다.
6 ‑ (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에 있는 값이 하나만 있습니다.
미리 감사합니다!
참조 솔루션
방법 1:
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);
}
방법 2:
NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];
For more details follow: https://developer.apple.com/reference/uikit/uitableview/1615000‑indexpathforselectedrow
Hope this helps.
방법 3:
You can use below code to get the indexpath and from this indexpath you can get also the selected cell.
let indexPath = self.tblObj.indexPathForSelectedRow!
let currentCell = self.tblObj.cellForRowAtIndexPath(indexPath)! as! YourTableViewCell
(by Muju、User511、Mrunal、Sarabjit Singh)