2013-02-19 170 views
2

我是Obj-C的新手。我一直在使用UICollectionView的排序功能。細胞轉移後我正在保存數據。但是NSIndexPath信息並不像我預期的那樣。我不確定在使用indexPathsForVisibleItems時如何訪問索引的一部分。在使用indexPathsForVisibleItems時只獲取NSIndexPath的一部分

NSArray *visible = [self.collectionView indexPathsForVisibleItems]; 
NSLog(@"These are the visible cells %@", visible); 

這將返回這樣NSLogs ....

"<NSIndexPath 0x8a6b7f0> 2 indexes [0, 0]", 
"<NSIndexPath 0x8a6d3d0> 2 indexes [0, 2]", 
"<NSIndexPath 0x8a6d480> 2 indexes [0, 3]", 
"<NSIndexPath 0x889f080> 2 indexes [0, 1]" 

我想要得到的數字的第二列。 NSIndex路徑中的0,2,3,1。我確信如果我能夠訪問這些值,我就可以從單元中獲取信息。是否有一個特定的約定或轉換,以獲得0,2,3,1和一個簡單的數組。

謝謝。

回答

3

你可以做這樣的事情......

NSArray *visible = [self.collectionView indexPathsForVisibleItems]; 
NSMutableArray *rowsArray = [NSMutableArray arrayWithCapacity:[visible count]]; 
[visible enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) { 
    [rowsArray addObject:@(indexPath.item)]; 
}]; 

這會給你代表你的可見項從indexPaths的所有項值NSNumber對象的數組。

如果你使用這個從UICollectionView你真的不應該使用NSIndexPath.row財產,因爲這是旨在與UITableView使用。 NSIndexPath UIKit Additions以供參考。

+0

哇!正確地粘貼它,它像魅力一樣工作。我敢打賭,我現在可以得到我想要的信息,即我已經得到了索引的後半部分。非常感激。謝謝。 – RasterGod 2013-02-19 07:30:08

+1

如果它解決/回答你的問題,你可以接受它作爲答案。謝謝! :-) – rickerbh 2013-02-19 08:39:03

+0

我是新來的stackoverflow。我當然會。 =) – RasterGod 2013-02-19 10:41:59

2
NSMutableArray * array = [NSMutableArray array]; 
NSArray * arr = [self.collectionView indexPathsForVisibleItems]; 
for(NSIndexPath * path in arr) { 
    [array addObject:[NSNumber numberWithInteger:path.section]]; 
    //(or) 
    [arr addObject:[NSNumber numberWithInteger:path.row]]; 
}