UITableView 到 UICollectionView 与 prepareForSegue 没有被调用 [英] UITableView to UICollectionView with prepareForSegue not getting called

查看:31
本文介绍了UITableView 到 UICollectionView 与 prepareForSegue 没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此还是有点陌生​​;所以要温柔.我想从 flickr 下载一个 setlist,然后选择以下 set 并查看它的图片.Sp far 我已经签约了一种获取演出单的方法.现在我希望能够单击集合列表之一并移动到 UICollectionsView 并查看其图像.我需要帮助才能选择 UITableView 中的行.我想我在某处遗漏了一步,但似乎找不到我遗漏的洞.

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.

无论如何这是代码

- (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: 方法:

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: 方法在每次按下行时被表视图调用.您可以使用它来处理行按下事件,也可以通过故事板来完成.在故事板中,您可以控制从表视图行到其他视图控制器的拖动以设置转场,在这种情况下,您甚至不需要使用 didSelectRowAtIndexPath: 方法,如果您需要在视图控制器之间传递一些数据,prepareForSegue: 就足够了.另一种方法是控制从表视图控制器(而不是行)到另一个视图控制器的拖动.在这种情况下,您需要以某种方式调用此 segue.我相信您以第二种方式设置了您的转场.要在按下行时调用它,请将这一行添加到 didSelectRowAtIndexPath: 方法,将上面代码中的注释替换为:

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]

//扩展

如果你想将数据传递给另一个视图,你可以使用第二个参数——sender.您可以在那里传递 require 对象或任何其他对象,例如:

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]
    }

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

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.

这篇关于UITableView 到 UICollectionView 与 prepareForSegue 没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆