-1

如果有人NSString需要一个用户 ID 用作请求的 URL:

有一个NSMutableArray他想一次一个地加入上述呼叫?所以基本上NSStringNSMutableArray.

可以检查多个 UITableView 单元格,一旦完成,我可以索引哪些单元格行被推送。这就是现在使用的 userIDArray 我想用从 userIDArray 返回的用户 ID 进行呼叫。

for (NSDictionary* userIDDict in userIDArray)
{   
    userIDArray = [[NSMutableArray alloc] init]; //I put this line in my viewdidload
    NSNumber* userID = [userIDDict objectForKey:@"UserID"];
}

UserIDArray 是NSMutableArray.

这将是NSLog来自NSMutableArrayThe Integer 的 1、2 和 3。

UserID: 1
UserID: 2
UserID: 3

因此,换句话说,我想将NSMultiTableArray1,2 和 3 的结果用于NSString

NSString *userProfile = [NSString stringWithFormat:@"http://example.com/userid=1"];

NSString *userProfile = [NSString stringWithFormat:@"http://example.com/userid=2"];

NSString *userProfile = [NSString stringWithFormat:@"http://example.com/userid=3"];

所以我会打第一个电话并等待结果,然后是第二个,最后是第三个。

这可以做到吗?我已经搜索了这个关于队列和这个链接,但我不确定这些是否是我需要的?

UserDetailViewController.h 文件:

@interface UserDetailViewController : UIViewController <UITableViewDelegate>{

long long expectedLength;
long long currentLength;
UITableView *userTableView;
NSIndexPath* checkedIndexPath;

}

@property (nonatomic, retain) NSArray *userIDJson;
@property (strong, nonatomic) NSDictionary *userIDDict;
@property (nonatomic, retain) NSIndexPath* checkedIndexPath;
@property (nonatomic, strong) NSMutableArray *userIDArray;
@property (nonatomic) NSInteger currentUserIndex;

@end

UserDetailViewController.m 文件:

@interface UserDetailViewController ()

@end

@implementation UserDetailViewController
@synthesize userIDJson;
@synthesize userIDDict;
@synthesize checkedIndexPath;
@synthesize userIDArray;
@synthesize currentUserIndex;

- (void)viewDidLoad
{
[super viewDidLoad];

userIDArray = [[NSMutableArray alloc] init];

[self.navigationController setNavigationBarHidden:NO];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//return self.loadedSearches.count;
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.userIDJson.count;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
}

cell.textLabel.text = self.userIDJson[indexPath.row][@"UserName"];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

if([self.checkedIndexPath isEqual:indexPath])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}

return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *userStringIndex = [self.userIDJson objectAtIndex:indexPath.row];
if (thisCell.accessoryType == UITableViewCellAccessoryNone)
{
    thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
    [userIDArray addObject:userStringIndex];
}
else
{
    thisCell.accessoryType = UITableViewCellAccessoryNone;
    [userIDArray removeObject:userStringIndex];

}

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

if (self.currentUserIndex < userIDArray.count) {

NSNumber* userID = [[userIDArray objectForIndex:currentUserIndex]objectForKey:@"UserID"];

//Make the actual request here, and assign the delegate.
NSString *userProfile = [NSString stringWithFormat:@"http://example.com/userid=%@",userID];

self.currentUserIndex++;

NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:userProfile]];

NSString *userResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];

NSURL *url = [[NSURL alloc] initWithString: userProfile];

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

userIDJson = [NSJSONSerialization JSONObjectWithData:dataURL 
                                            options:kNilOptions
                                              error:&error];
  }

 }

for (userIDDict in userIDArray)
{
    NSNumber* userID = [userIDDict objectForKey:@"UserID"];
    NSLog(@"%@", userID);
    NSArray* userName = [userIDDict objectForKey:@"UserName"]; 
}
4

1 回答 1

0

NSURLConnection可以通过构造函数接受委托initWithRequest:delegate:。因此,您需要使调用符合该协议的对象,我假设它是一个UIViewController. 您可以使用委托中的一种必需方法来启动下一个请求。

例如,假设您具有指示当前索引的属性。

@property (nonatomic) NSInteger currentUserIndex;

然后在将触发第一个请求的地方,为第一个用户拨打电话。在一些委托方法中,说connectionDidFinishLoading:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    if (self.currentUserIndex < self.userIDArray.count) {
        NSNumber* userID = [[self.userIDArray objectAtIndex:self.currentUserIndex] objectForKey:@"UserID"];
        self.currentUserIndex++;
        //Make the actual request here, and assign the delegate.
    }
}

当然,如果你的连接调用不必是同步的,你可以用更简单的方法来做。

于 2013-05-31T22:42:14.830 回答