2014-02-18 62 views
0

我有UIcollectionview与部分。在cellForItemAtIndexPath我可以看到indexPath.sectionindexPath.item。我如何知道第一节开头的项目号码?UICollectionview项目编号

回答

2

没有内置的方法。但你可以在当前部分中的所有先前 部分项目的数量增加了项目编号:

NSInteger num = indexPath.item; 
for (NSInteger section = 0; section < indexPath.section; section++) { 
    num += [collectionView numberOfItemsInSection:section]; 
} 

不过,或许你也应该检查你是否真的需要这一点。例如,如果原因是 您的数据源是平面阵列,那么您可以考虑重新排列数据源 ,以反映两级部分/项目结构。

-1

您可能正在寻找indexPath.row

+1

indexPath.row和indexPath.item是相同的。 – Alexey

+0

他们是不一样的;)导致相同的结果(至少我没有经历任何区别),但最初indexPath.row用于UITableViews和.item引入并用于UICollectionViews(请参阅[这里的详细信息] (https://developer.apple.com/library/ios/documentation/uikit/reference/NSIndexPath_UIKitAdditions/Reference/Reference.html) – HAS

0

您需要在indexPath(0,0)的细胞,所以尝试:

NSIndexPath * indexPath = [NSIndexPath indexPathForItem: 0 inSection: 0]; 

,然后用cellForItemAtIndexPath像以前