Sam*_*cer 5 objective-c nsindexpath ios uicollectionview
使用UICollectionView时,我很难获得方法的indexPath值didSelectItemAtIndexPath.
我在方法中使用以下代码行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);
Run Code Online (Sandbox Code Playgroud)
问题是indexPath总是返回0(参见下面的代码),因此只选择了FileList NSArray中的第一项.
我尝试过不同的参数,比如
indexPath.rowindexPath.itemindexPath所有这些在以下NSLog语句中返回值0:
NSLog(@"index path: %d", indexPath.item); //I also tried indexPath.row here
Run Code Online (Sandbox Code Playgroud)
也许我只是不正确地格式化NSString,但我不认为这是因为没有警告,我尝试在其他地方以不同方式格式化它.
为什么indexPath总是返回0?
任何帮助,将不胜感激!谢谢!
zja*_*mes 30
冒着明显的风险,确保你的代码在didSelectItemAtIndexPath方法而不是didDeselectItemAtIndexPath.indexPath在后一种方法中,您将获得给定触摸事件的非常不同的值,并且很容易在Xcode的代码完成时插入错误的值.
您使用的委托方法始终提供所选单元格的索引路径。尝试在 NSLog 调用上使用断点进行调试。当调试器停止时,查看底部的调试区域并使用控制台(查看=>调试区域=>激活控制台,如果需要的话)。
在控制台中输入:po indexPath
如果所选项目是第 5 个部分中的列表,您应该看到类似这样的内容:0(第一部分,可能是唯一的部分)
<NSIndexPath 0xa8a6050> 2 indexes [0, 5]
希望这有助于弄清楚发生了什么事。