gpt4 book ai didi

ios - 获取 UICollectionViewCell 标签文本

转载 作者:行者123 更新时间:2023-11-29 11:59:47 24 4
gpt4 key购买 nike

我当前执行到我的下一个 View Controller 的代码如下:

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

}
}

我遇到的问题是如何分别从每个选定的单元格中获取我的文本值,以便我可以将其作为另一个标签发布到详细 View Controller 中。

我的 prepare for segue 中的评论行准确地描述了我想要实现的目标。我只是想获取单个 UICollectionViewCell Label.text 的值

这可能看起来类似于以前通过 View Controller 帖子传递数据,但这与我发现的任何内容都不同,因为在这些帖子中,文本值是常量,即 label.text 的值设置为一件事并且不是来自数组。

我只是想知道如何找到单独选择的单元格的标签值并将其传递给我的详细 View Controller 。

I Have updated the code following examples giving and still no result as the picture does not show.

最佳答案

要在 segue 期间将信息传递给下一个 View Controller ,您可以使用 destinationViewController segue 上的属性(property)传递给 prepareForSegue:sender: 的参数.当然,您必须在该目标 View Controller 上设置属性才能设置值。

要确定用户选择了哪些信息,您有几个选项。您可以在 View Controller 上创建一个属性来存储用户选择的内容,并在 collectionView:didSelectItemAtIndexPath: 期间将该值放入属性中。基于 indexPath参数,或者您可以使用 UICollectionView方法 indexPathsForSelectedItemsprepareForSegue:sender: 期间获取所选项目的索引路径.我倾向于后者。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"GroupsHomeSegue"])
{
NSIndexPath* indexPath = [[someCollectionView indexPathsForSelectedItems] first];
if(indexPath != nil)
{
NSString* selectedDescription = arrayOfDescriptions[indexPath.item];
NSString* selectedImageName = arrayOfImages[indexPath.item];

// Get the destination view controller (the one that will be shown) from the segue and cast it to the appropriate type. Assuming this should be GroupsHomeViewController, but I'm not entirely sure that's correct since I can't see all your code
GroupsHomeViewController* groupsHomeViewController = segue.destinationViewController;
// Set the appropriate properties (Again, I'm guessing here since I can't see your code)
groupsHomeViewController.logoImage = [UIImage imageNamed: selectedImageName];
}
}
}

关于ios - 获取 UICollectionViewCell 标签文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37446103/

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