我执行segue到下一个视图控制器的当前代码如下:
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [arrayOfDescriptions count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
[[cell IconImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
[[cell IconLabel]setText:[arrayOfDescriptions objectAtIndex:indexPath.item]];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"GroupsHomeSegue" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"GroupsHomeSegue"])
{
//IconImage is the name of the image in GroupsViewController i want to pass through. Based upon what each cell is set at anyways.
//logoImage is the name of the image i want to set in GroupsHomeViewController.
//so basically i want to be able to get IconImage and set it as logoImage in a different view controller
}
}
我遇到的问题是,如何从每个选定的单元格中单独获取文本的值,以便我可以将其作为另一个标签发布到详细的视图控制器中。
我准备segue中的注释行描述了我想要实现的目标。我只想获取单个UICollectionViewCell Label.text
的值这可能看起来类似于之前通过视图控制器帖子传递数据,但这与我发现的任何内容都不同,因为在这些帖子中文本值是常量,即label.text的值设置为一件事而且不是来自阵列。
我只是想知道如何找到单独选择的单元格的标签值并将其传递给我的详细视图控制器。
答案 0 :(得分:1)
要在segue期间将信息传递给下一个视图控制器,您可以使用传递给destinationViewController
的{{1}}参数上的segue
属性。您必须在该目标视图控制器上设置属性才能设置值。
要确定用户选择的信息,您有几个选择。您可以在视图控制器上创建一个属性,以存储用户选择的内容,并根据prepareForSegue:sender:
参数在collectionView:didSelectItemAtIndexPath:
期间将该值放入属性中,或者您可以使用indexPath
方法{ {1}}在UICollectionView
期间获取所选项目的索引路径。我倾向于做后者。
indexPathsForSelectedItems
答案 1 :(得分:0)
在“didSelectRowAtIndexPath”中将indexPath作为“sender”传递(请参阅下面的代码)。在“performSegueWithIdentifier”中,您将获得所选单元格的indexPath。
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@"GroupsHomeSegue" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"GroupsHomeSegue"])
{
NSIndexPath *indexPath = (NSIndexPath*)sender;
NSString* iconImage = arrayOfImages[indexPath.row];
}
}
答案 2 :(得分:0)
尝试使用此代码 -
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"GroupsHomeSegue" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"GroupsHomeSegue"])
{
UICollectionViewCell *cell = [self.collectionView cellForItemAtindexPath:sender];
NSString *text = cell.IconLabel.text;
UIImage *image = cell.IconImage.image;
GroupsHomeViewController* ghVC = segue.destinationViewController;
groupsHomeViewController.logoImage = image;
}
}