我在一个UICollectionView中创建了一个ViewController。我的UiCollectionViewCell包含一个图像视图和一个标签。看起来处理点击事件真的很糟糕。我需要点击很多次,然后它才会对点击做出反应。通常情况下,它只在双击时才这样做。
- (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;
}
}这是正常的行为还是我遗漏了什么?
发布于 2014-01-19 20:50:00
当单击单元格时,您正在执行同步请求,这可能会导致您不得不多次单击的“感觉”。我建议您将NSURLConnection同步请求更改为异步请求。如下所示:
[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);
}
}];https://stackoverflow.com/questions/21221871
复制相似问题