ios UICollection View - Segue - 确实选择了特定的单元格,LOG
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12677775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UICollection View - Segue - did select specific cell , LOG
提问by Knitsu
I am starting to use a UICOllectionview to load a custom made photo album, the album loads just fine, and the data is coming from my server (SQL-JSON-NSARRAY-NSDictionary) and been populated in the cell's just fine, However now I would like it when the user selects that cell to load a new UIVIewController with that image in full size (so they can view/print/share etc)
我开始使用 UICOllectionview 加载自定义相册,相册加载得很好,数据来自我的服务器 (SQL-JSON-NSARRAY-NSDictionary) 并填充在单元格中就好了,但是现在我当用户选择该单元格以加载带有该图像的全尺寸新 UIVIewController 时会喜欢它(以便他们可以查看/打印/共享等)
However I am getting stuck in the 'prepare for segue' method, as I cannot pull any information from that specific cell
但是,我陷入了“准备转场”方法的困境,因为我无法从该特定单元格中提取任何信息
as a test right now I just want a NSLOG to say "the User selected TITLE OF ALBUM %@ "
作为现在的测试,我只想让 NSLOG 说“用户选择了专辑标题 %@”
Here is my code thus far, any help would greatly be appreciated...
到目前为止,这是我的代码,任何帮助将不胜感激......
#import "AlbumViewController.h"
#import "AlbumCustomCell.h"
#import "PhotosInAlbumsViewController.h"
@interface AlbumViewController ()
@end
@implementation AlbumViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
/// Grab Photo Album information (pics,title,date) From Server
NSString *myURL1 = [ NSString stringWithFormat: @"http://mywebsiteaddress.com/index.php"];
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:myURL1]];
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSArray *items = [json objectForKey:@"users"];
photoalbumarray = items;
// NSLog(@"%@", photoalbumarray);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [photoalbumarray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"CelliD";
AlbumCustomCell *myCell = (AlbumCustomCell *) [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
// this above states to setup the cell using the Identifier I specified in Sotry board, so that cell is constantly reused
if (indexPath.section == 0) {
NSDictionary *item = [photoalbumarray objectAtIndex:[indexPath row]];
myCell.titleincell.text = [item objectForKey:@"title"]; // this puts the title in the cell
teststring = [item objectForKey:@"title"];
myCell.dateincell.text = [item objectForKey:@"date"]; // this puts the date in the data label in the cell
// myCell.imageincell1.image = [UIImage imageNamed:[item objectForKey:@"img1"]]; // this puts the picture in the cell
myCell.imageincell2.image = [UIImage imageNamed:[item objectForKey:@"img2"]]; // this puts the picture in the cell
myCell.imageincell3.image = [UIImage imageNamed:[item objectForKey:@"img3"]]; // this puts the picture in the cell
// IMAGE 1
NSString *picture1 = [item objectForKey:@"img1"];
NSURL *imageURL1 = [NSURL URLWithString:picture1];
NSData * imageData1 = [NSData dataWithContentsOfURL:imageURL1];
UIImage * image1 = [UIImage imageWithData:imageData1];
myCell.imageincell1.image = image1;
myCell.imageincell1.alpha = 0.0;
[UIView animateWithDuration:1.5 animations:^{
myCell.imageincell1.alpha = 1.0;
}];
// IMAGE 2
NSString *picture2 = [item objectForKey:@"img2"];
NSURL *imageURL2 = [NSURL URLWithString:picture2];
NSData * imageData2 = [NSData dataWithContentsOfURL:imageURL2];
UIImage * image2 = [UIImage imageWithData:imageData2];
myCell.imageincell2.image = image2;
myCell.imageincell2.alpha = 0.0;
[UIView animateWithDuration:1.5 animations:^{
myCell.imageincell2.alpha = 1.0;
}];
// I MAGE 3
NSString *picture3 = [item objectForKey:@"img3"];
NSURL *imageURL3 = [NSURL URLWithString:picture3];
NSData * imageData3 = [NSData dataWithContentsOfURL:imageURL3];
UIImage * image3 = [UIImage imageWithData:imageData3];
myCell.imageincell3.image = image3;
myCell.imageincell3.alpha = 0.0;
[UIView animateWithDuration:1.5 animations:^{
myCell.imageincell3.alpha = 1.0;
}];
}
return myCell;
}
// SEGUE TRANSITION FROM ALBUMS TO PHOTO
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showPhotoAlbum"]) {
NSLog(@"Prepare for Segue to load Specific Photo Album");
NSLog(@"User Selected this cell or the name of the title in the cell which comes from a NSDictionary =>");
// EXAMPLE NSLOG TO SHOW => User Selected - Album Name
// Where album name is actually the album name grabbed form the server based on the selected row
}
}
@end
and just so you can see my JSON data from the server coming in, here that is
这样你就可以看到来自服务器的我的 JSON 数据,这里是
2012-10-01 13:02:53.856 CB Photo[3957:907] (
{
date = "2012-10-01 07:23:01";
id = 1;
img1 = "http://b2fdev.ca/demo/CBPhoto/pic1.jpg";
img2 = "http://b2fdev.ca/demo/CBPhoto/pic2.jpg";
img3 = "http://b2fdev.ca/demo/CBPhoto/pic3.jpg";
title = "Paul and Jenna's Wedding 2012";
},
{
date = "2012-10-01 05:23:23";
id = 2;
img1 = "http://core1.ca/wp-content/themes/custom-community/images/block4.png";
img2 = "http://core1.ca/wp-content/themes/custom-community/images/block3.png";
img3 = "http://core1.ca/wp-content/themes/custom-community/images/block2.png";
title = "Core 1";
}
)
回答by kafejo
You mean this?
你是这个意思?
NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems];
DetailViewController *destViewController = segue.destinationViewController;
NSIndexPath *index = [indexPaths objectAtIndex:0];
NSLog(@"%d", index.section * 2 + index.row);
this print index in array in 2 x whatever array;
此打印索引在 2 x 任何数组中的数组中;
回答by Jere K?pyaho
The way I do it is keep a private member variable in my collection view controller:
我这样做的方法是在我的集合视图控制器中保留一个私有成员变量:
NSInteger selectedPhotoIndex;
It gets updated every time the user selects something in the collection view:
每次用户在集合视图中选择某些内容时,它都会更新:
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
selectedPhotoIndex = indexPath.row;
}
Then I just use it in my prepareForSegue
method. Applied to your case it would look something like this:
然后我只是在我的prepareForSegue
方法中使用它。应用于您的案例,它看起来像这样:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"ShowPhotoAlbum"]) {
NSLog(@"User selected %@", [[photoalbumarray objectAtIndex:selectedPhotoIndex] objectForKey:@"title");
// ... find the right detail VC, set its data, set up delegate etc.
}
}
This is pretty simple because in this case I only have one section in the collection view. If you have several sections, just maintain an NSIndexPath
instance, instead of a simple integer.
这很简单,因为在这种情况下,我在集合视图中只有一个部分。如果您有多个部分,只需维护一个NSIndexPath
实例,而不是一个简单的整数。
回答by foOg
To get the selected element in your datasource you can use:
要在数据源中获取所选元素,您可以使用:
[self.collectionView indexPathsForSelectedItems];
It returns an array of indexPath. Then you can configure the destination view controller like that:
它返回一个 indexPath 数组。然后你可以像这样配置目标视图控制器:
NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems];
ViewController *destinationViewController = (ViewController *)segue.destinationViewController;
destinationViewController.myPropertyToSet = [self.photoalbumarray objectAtIndex:[[test objectAtIndex:0] row]]; // You may have to store it in a proper NSindexPath object before or cast it
I get only the index 0 but you can request any index returned in indexPaths array. Also don't forget to be sure that the array is not empty (normally it should not but just in case)
我只得到索引 0,但您可以请求 indexPaths 数组中返回的任何索引。另外不要忘记确保数组不为空(通常不应该,但以防万一)
回答by foOg
You can also use the sender parameter of
您还可以使用 sender 参数
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
As sender will be the UICollectionViewCell, or subclass, that started the segue.
发送者将是 UICollectionViewCell 或子类,它启动了 segue。
This can help clean up your code.
这可以帮助清理您的代码。
So as an example for the code provided for in the original question
因此,作为原始问题中提供的代码的示例
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
[super prepareForSegue:segue sender:sender];
id destination = segue.destinationViewController;
if ([sender isKindOfClass:[AlbumCustomCell class]])
{
AlbumCustomCell *cell = sender;
// set something on destination with a value from the cell
}
}