gpt4 book ai didi

objective-c - UICollectionView 的 indexPath 值

转载 作者:可可西里 更新时间:2023-11-01 04:04:45 24 4
gpt4 key购买 nike

当使用 UICollectionView 时,我对获取 didSelectItemAtIndexPath 方法的 indexPath 值感到困惑。

我在 didSelectItemAtIndexPath 方法中使用了以下代码行:

//FileList is an NSArray of files from the Documents Folder within my app.
//This line gets the indexPath of the selected item and finds the same index in the Array
NSString *selectedItem = [NSString stringWithFormat:@"%@",[FileList objectAtIndex:indexPath.item]];

//Now the selected file name is displayed using NSLog
NSLog(@"Selected File: %@", selectedItem);

问题是 indexPath 总是返回 0(见下面的代码),因此只有 FileList NSArray 中的第一个项目被选中。

我尝试了不同的参数,例如

  • indexPath.row
  • indexPath.item
  • 只是简单的 indexPath

在以下 NSLog 语句中,所有这些都返回值 0:

NSLog(@"index path: %d", indexPath.item); //I also tried indexPath.row here

也许我只是不正确地格式化了 NSString,但我认为情况并非如此,因为没有警告,我已经尝试在其他地方以不同的方式格式化它。

为什么indexPath总是返回0?

任何帮助将不胜感激!谢谢!

最佳答案

冒着陈述显而易见的风险,确保您的代码在 didSelectItemAtIndexPath 方法中,而不是在 didDeselectItemAtIndexPath 中。对于后一种方法中的给定触摸事件,您将获得非常不同的 indexPath 值,并且很容易使用 Xcode 的代码完成插入错误的值。

关于objective-c - UICollectionView 的 indexPath 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12761040/

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