UITableView to UICollectionView with prepareForSeg

2019-06-05 02:25发布

问题:

I'm still kinda new to this; so be gentle. I want to download a setlist from flickr then select the following set and view its pictures. Sp far I've contracted a way to get setlist. Now I want to be able to click one of the set lists and move to UICollectionsView and view its images. I need help with being able to select the row in the UITableView. I think I'm missing a step somewhere but can't seem to find the hole I'm missing.

Anyway here's the code

- (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;

}

回答1:

If you want to push/present new view after you press row in table view you need to implement tableView:didSelectRowAtIndexPath: method:

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

didSelectRowAtIndexPath: method is called by table view every time you press the row. You can use it to handle the row press event or you can do it via storyboard. In storyboard you can control drag from table view row to other view controller to set up segue, in that scenario you don't even need to use didSelectRowAtIndexPath: method, prepareForSegue: is enough if you need to pass some data between view controllers. The other way is control drag from table view controller (not row) to another view controller. In that case you need to call this segue in some way. I believe you set up your segue in the second way. To call it when you press the row add this line to didSelectRowAtIndexPath: method, replace comment from code above with:

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

//EXTENDED

If you want to pass a data to the other view you can use the second parameter - sender. You can pass require object or any other object there, for example:

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

And in prepareForSeque: method you can retrieve this object and use it:

-(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);
    }
}

This is an example you can ament to your needed.