-2

UICollectionView在一个屏幕上使用 2 并从我的 api 获取 2 个不同的数据集。

但问题是我无法解决根据请求的视图区分传入数据。

这是我在 中所做的代码片段UICollectionView

- (void)viewDidLoad
{

    [super viewDidLoad];

    NSURL *headlineurl = [NSURL URLWithString:@"api1"];
    headlinerequest = [NSURLRequest requestWithURL:headlineurl];
    [[NSURLConnection alloc] initWithRequest:headlinerequest delegate:self];

    NSURL *mostnewsurl = [NSURL URLWithString:@"api2"];
    NSURLRequest *mostnewsrequest = [NSURLRequest requestWithURL:mostnewsurl];
    [[NSURLConnection alloc] initWithRequest:mostnewsrequest delegate:self];

}

这是代表的代码:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
   //What should I do here ?
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
   //What should I do here ?
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   //What should I do here ?
}

非常感谢。

4

1 回答 1

1

NSUrlConnection当人们开始使用委托时,您的问题很常见,

首先,您将两个视图的委托设置为同一个对象,这可以工作,但需要一些技巧。

我推荐以下解决方案之一:

解决方案 1(使用委托,更多工作)

创建一个新类,并给它一个NSURlconnection 委托协议 ,并将其命名为apiFetchDelegate

然后将您的委托方法放在那里-(void) connectionDidFinishLoading等。

现在在您的viewDidLoad方法中,将其更改为以下内容:

NSURL *headlineurl = [NSURL URLWithString:@"api1"];
headlinerequest = [NSURLRequest requestWithURL:headlineurl];

//Create a new instance of the delegate
apiFetchDelegate* headlineDelegate = [[apiFetchDelegate alloc] init];
[[NSURLConnection alloc] initWithRequest:headlinerequest delegate:headlineDelegate];

第二位代表:

NSURL *mostnewsurl = [NSURL URLWithString:@"api2"];
NSURLRequest *mostnewsrequest = [NSURLRequest requestWithURL:mostnewsurl];
//Create second delegate
apiFetchDelegate* mostnewsDelegate = [[apiFetchDelegate alloc] init];
[[NSURLConnection alloc] initWithRequest:mostnewsrequest delegate:mostnewsDelegate];

现在如您所见,每个人都将获得自己的委托,并且数据不会再混合了!

解决方案 2(没有代表,要做的工作更少)

这可能是满足您需求的更好解决方案,我不确定为什么您需要代表来进行如此简单的调用,但如果您不需要,最好采用这种简单的方式!

我们将进行异步调用以避免在获取数据时冻结 UI,这将需要一个NSOperationQueue,这是它的工作方式:

在您的 viewDidLoad 方法中,将代码更改为:

//Create your Queue here
NSOperationQueue *apiCallsQueue = [NSOperationQueue alloc] init];
[apiCallsQueue setMaxConcurrentOperations:2];

NSURL *headlineurl = [NSURL URLWithString:@"api1"];
headlinerequest = [NSURLRequest requestWithURL:headlineurl];

[NSURLConnection sendAsynchronousRequest:headlinerequest queue:apiCallsQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    //Here is your data for the first view
    //
    NSLog(@"Data for headline view: %@", [[NSString alloc] initWithData:data
                                       encoding:NSUTF8StringEncoding]);
}];

对于第二种观点:

NSURL *mostnewsurl = [NSURL URLWithString:@"api2"];
NSURLRequest *mostnewsrequest = [NSURLRequest requestWithURL:mostnewsurl];
[NSURLConnection sendAsynchronousRequest:mostnewsrequest queue:apiCallsQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    //Here is your data, for the second view
    //
    NSLog(@"Date for latest news: %@", [[NSString alloc] initWithData:data
                                       encoding:NSUTF8StringEncoding]);
}];

让我知道这是否适合您,或者您是否需要进一步的帮助。

于 2013-06-13T21:45:27.783 回答