首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UICollectionView Tap事件

UICollectionView Tap事件
EN

Stack Overflow用户
提问于 2014-01-19 19:58:29
回答 1查看 916关注 0票数 0

我在一个UICollectionView中创建了一个ViewController。我的UiCollectionViewCell包含一个图像视图和一个标签。看起来处理点击事件真的很糟糕。我需要点击很多次,然后它才会对点击做出反应。通常情况下,它只在双击时才这样做。

代码语言:javascript
复制
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{


    NSString *urlString2 =  [NSString stringWithFormat:@"http://ratemyplays.com/songs.php?listid=%d", indexPath.row];
    NSMutableURLRequest *request2 = [[NSMutableURLRequest alloc]init];
    [request2 setTimeoutInterval:20.0];
    [request2 setURL:[NSURL URLWithString:urlString2]];
    [request2 setHTTPMethod:@"POST"];

    NSString *PHPArray = [[NSString alloc] initWithData:[NSURLConnection sendSynchronousRequest:request2 returningResponse:nil error:nil] encoding:NSUTF8StringEncoding];

    NSArray *playArray2 = [PHPArray componentsSeparatedByString:@"."];

    if ([playArray2 count] <2) {
        UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"This Playlist is empty!!" message:@"We're Currently modifying it" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

        [alert2 show];
    } else {
        YouTubeTableViewController *youTubeTableViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"YouTubeTableViewController"];
        youTubeTableViewController.selectedRowValue=indexPath.row;
        [self.navigationController pushViewController:youTubeTableViewController animated:YES];
        youTubeTableViewController.titleName = scoreArray;
        youTubeTableViewController.playlistId = scoreArray2;
}


}

这是正常的行为还是我遗漏了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-19 20:50:00

当单击单元格时,您正在执行同步请求,这可能会导致您不得不多次单击的“感觉”。我建议您将NSURLConnection同步请求更改为异步请求。如下所示:

代码语言:javascript
复制
[NSURLConnection sendAsynchronousRequest:request
                                   queue:[[NSOperationQueue alloc] init]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {

     if (error == nil)
     {
         NSString *PHPArray = =[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
         NSLog(@"data received from url: %@", PHPArray);
         if ([playArray2 count] <2) {
             UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"This Playlist is empty!!" message:@"We're Currently modifying it" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

             [alert2 show];
         } else {
             YouTubeTableViewController *youTubeTableViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"YouTubeTableViewController"];
             youTubeTableViewController.selectedRowValue=indexPath.row;
             [self.navigationController pushViewController:youTubeTableViewController animated:YES];
             youTubeTableViewController.titleName = scoreArray;
             youTubeTableViewController.playlistId = scoreArray2;
         }

     }
     else if (error != nil && error.code == NSURLErrorTimedOut)
     {
         NSLog(@"error code: %ld", (long)error.code);
     }
     else if (error != nil)
     {
         NSLog(@"error code: %ld", (long)error.code);
     }
 }];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21221871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档