UITableView到UICollectionView与prepareForSegue没有得到调用

我对这个还是比较新鲜的。 所以要温柔。 我想从flickr下载一个设置列表,然后select下面的设置并查看它的图片。 很远我签订了一个方法来获取setlist。 现在我想能够点击其中一个列表并移动到UICollectionsView并查看它的图像。 我需要能够在UITableViewselect行的帮助。 我想我错过了某个步骤,但似乎无法find我失踪的洞。

无论如何,这是代码

 - (void)viewDidLoad { [super viewDidLoad]; self.tableView.rowHeight = 44; photoURLs = [[NSMutableArray alloc] init]; photoSetNames = [[NSMutableArray alloc] init]; photoids = [[NSMutableArray alloc] init]; [self loadFlickrPhotos]; } - (void)loadFlickrPhotos { // 1. Build your Flickr API request w/Flickr API key in FlickrAPIKey.h NSString *urlString = [NSString stringWithFormat:@"http://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=%@&user_id=%@&per_page=10&format=json&nojsoncallback=1", FlickrAPIKey, @"62975213@N05"]; NSURL *url = [NSURL URLWithString:urlString]; // 2. Get URLResponse string & parse JSON to Foundation objects. NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; // NSDictionary *results = [jsonString JSONValue]; NSDictionary *results = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]; // 3. Pick thru results and build our arrays NSArray *photosets = [[results objectForKey:@"photosets"] objectForKey:@"photoset"]; for (NSDictionary *photoset in photosets) { // 3.a Get title for e/ photo NSString *title = [[photoset objectForKey:@"title"] objectForKey:@"_content"]; [photoSetNames addObject:(title.length > 0 ? title : @"Untitled")]; NSString *photoid = [photoset objectForKey:@"id"]; [photoids addObject:photoid]; } } -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if([segue.identifier isEqualToString:@"viewGallery"]){ flickrGalleryViewController *controller = (flickrGalleryViewController *)segue.destinationViewController; NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; controller.photoids = [photoids objectAtIndex:indexPath.row]; NSLog(@"photoid = %@", controller.photoids); } } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [photoSetNames count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell Identifier"]; cell.textLabel.text = [photoSetNames objectAtIndex:indexPath.row]; return cell; 

}

如果要在按表格视图中的行之后按下/呈现新视图,则需要实现tableView:didSelectRowAtIndexPath:方法:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Present new view here } 

didSelectRowAtIndexPath:方法在每次按行时都由表视图调用。 您可以使用它来处理行按事件,或者你可以通过故事板来做到这一点。 在storyboard中,你可以控制从表视图行拖到其他视图控制器设置segue,在这种情况下,你甚至不需要使用didSelectRowAtIndexPath:method,prepareForSegue:如果你需要在视图控制器之间传递一些数据就足够了。 另一种方式是控制从表视图控制器(不是行)拖到另一个视图控制器。 在这种情况下,你需要以某种方式调用这个segue。 我相信你以第二种方式设置你的继续。 要在按下行时调用它,请将此行添加到didSelectRowAtIndexPath:方法,用以上代码replace注释:

 [self performSegueWithIdentifier:@"YOURSEGUENAMEFORMSTORYBOARD" sender:nil] 

// EXTENDED

如果你想传递数据到另一个视图,你可以使用第二个参数 – 发件人。 您可以在那里传递require对象或任何其他对象,例如:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //I pass NSIndexPath to prepare for segue [self performSegueWithIdentifier:@"YOURSEGUENAMEFORMSTORYBOARD" sender: indexPath] } 

在prepareForSeque:方法中,你可以检索这个对象并使用它:

 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if([segue.identifier isEqualToString:@"viewGallery"]){ flickrGalleryViewController *controller = (flickrGalleryViewController *)segue.destinationViewController; // HERE I retrieve index path from parameter NSIndexPath *indexPath = (NSIndexPath*)sender; controller.photoids = [photoids objectAtIndex:indexPath.row]; NSLog(@"photoid = %@", controller.photoids); } } 

这是一个可以帮助你实现的例子。