gpt4 book ai didi

ios - 没有调用 prepareForSegue 的 UITableView 到 UICollectionView

转载 作者:行者123 更新时间:2023-12-01 17:40:44 25 4
gpt4 key购买 nike

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

无论如何,这是代码

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

}

最佳答案

如果要在表格 View 中按行后推送/呈现新 View ,则需要实现 tableView:didSelectRowAtIndexPath: 方法:

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

每次按下行时,表格 View 都会调用 didSelectRowAtIndexPath: 方法。
您可以使用它来处理行按下事件,也可以通过 Storyboard 来完成。在 Storyboard 中,您可以控制从 TableView 行拖动到其他 View Controller 以设置 segue,在这种情况下,您甚至不需要使用 didSelectRowAtIndexPath: 方法,如果您需要在 View Controller 之间传递一些数据,则 prepareForSegue: 就足够了。另一种方法是控制从 TableView Controller (而不是行)拖动到另一个 View Controller 。在这种情况下,您需要以某种方式调用此 segue。我相信你以第二种方式设置你的segue。
要在按下行时调用它,请将此行添加到 didSelectRowAtIndexPath: 方法,将上面代码中的注释替换为:
[self performSegueWithIdentifier:@"YOURSEGUENAMEFORMSTORYBOARD" sender:nil]

//扩展

如果您想将数据传递给另一个 View ,您可以使用第二个参数 - sender。您可以在那里传递 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);
}
}

这是一个您可以根据需要修改的示例。

关于ios - 没有调用 prepareForSegue 的 UITableView 到 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20442303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com