在使用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.rowindexPath.itemindexPath所有这些都在以下NSLog语句中返回值0:
NSLog(@"index path: %d", indexPath.item); //I also tried indexPath.row here也许我只是不正确地格式化NSString,但是我不认为是这样的,因为没有警告,我尝试过在其他地方对它进行不同的格式化。
为什么indexPath总是返回0?
任何帮助都将不胜感激!谢谢!
发布于 2012-12-14 14:39:26
冒着声明显而易见的风险,确保代码位于didSelectItemAtIndexPath方法中,而不是didDeselectItemAtIndexPath方法中。在后一种方法中,对于给定的触摸事件,您将得到非常不同的indexPath值,而且很容易在Xcode的代码完成过程中插入错误的值。
发布于 2012-10-27 14:44:09
您使用的委托方法总是提供所选单元格的索引路径。尝试使用NSLog调用上的断点进行调试。当调试器停止运行时,请查看底部的调试区域并使用控制台(如果需要,可以查看=>调试区域=>激活控制台)。
输入控制台:po indexPath
如果选中的项是节中列表的第5项,您应该会看到这样的情况:0(第一部分,可能是唯一的)。
<NSIndexPath 0xa8a6050> 2 indexes [0, 5]
希望这能帮我们弄清楚到底发生了什么。
https://stackoverflow.com/questions/12761040
复制相似问题