2014-02-18 60 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像以前