有沒有人知道NSIndexpath.row
和NSIndexpath.item
之間的區別?NSIndexpath.item vs NSIndexpath.row
具體說,一個做我使用:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
有沒有人知道NSIndexpath.row
和NSIndexpath.item
之間的區別?NSIndexpath.item vs NSIndexpath.row
具體說,一個做我使用:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
好了,沒有人在這裏賜給一個很好的答案。
在NSIndexPath中,索引存儲在一個名爲「_indexes」的簡單c數組中,定義爲NSUInteger *,數組長度存儲在定義爲NSUInteger的「_length」中。訪問器「section」是「_indexes [0]」的別名,「item」和「row」都是「_indexes [1]」的別名。因此這兩者在功能上是相同的。
在編程風格方面 - 也許是定義鏈 - 您最好在表格的上下文中使用「row」,在集合的上下文中使用「item」。
太棒了!所以實際上沒有任何區別。 – fatuhoku
我喜歡簡單的回答而不是解釋它是如何工作的等等。好工作 – Codenator81
人們對此的瞭解如何打擊我的想法,只是我一直在尋找的答案!非常感謝! – Dan
您需要使用indexPath.row
區別在於:
indexPath.row是的tableView和indexPath.item是collectionView。
項
索引號識別的集合圖的部分的項目。 (只讀)
@property (nonatomic, readonly) NSInteger item;
討論
的部分中的項目是在通過部的值來標識。 狀況
Available in iOS 6.0 and later.
宣佈UICollectionView.h
行
索引號識別在表視圖的截面的行。 (只讀)
@property(nonatomic, readonly) NSInteger row;
討論
的部分中的行是在通過部的值來標識。 可用性
Available in iOS 2.0 and later.
請檢查NSIndexPath Additions的細節
當你從UICollectionView {Delegate,DataSource}'訪問'row'時,或者如果你從'UITableView {Delegate,DataSource}'中訪問'item',會發生什麼? – fatuhoku
你得到一個訪問衝突或更糟 – iosengineer
它仍然會工作 – Lirik
indexPath.row is best in your case
約NSIndexPath
一是信息的NSIndexPath
類表示嵌套數組的樹路徑的特定節點集合。這條路徑被稱爲索引路徑。
indexPath中的每個索引都表示從樹中的一個節點到另一個較深節點的子數組的索引。
例如,indexPath 1.4.3。2指定你的情況indexPath.row
返回該行的在該特定indexPath
索引圖
這裏所示的路徑。indexPath.row and indexPath.item
indexPath
通常之間
差異具有兩個屬性
1 - 行
2 - 項
row - property with UITableView
for get specific row base on indexPath。 它也只讀屬性
Available in iOS 2.0 and later.
項目 -與UICollectionView
正確使用在部分得到項目。 這是一個只讀屬性。要使用這個屬性,你需要聲明它
UICollectionView.h
> Available in iOS 6.0 and later.
@Owen戈弗雷的回答比@iPatel接受的答案要好。這裏有一些進一步的說明,我無法將其納入對他的回答的評論,所以我會複製他的答案並將其添加到此處。信用屬於歐文。
從@Owen戈弗雷:
內部NSIndexPath,索引被存儲在稱爲定義爲「_indexes」定義爲NSUInteger *和數組的長度被存儲在「_length」一個簡單的C陣列NSUInteger。訪問器「節」是「_indexes [0]」的別名,「item」和「row」都是「_indexes 1」的別名。因此這兩者在功能上是相同的。
在編程風格方面 - 也許是定義鏈 - 您最好在表格的上下文中使用「row」,在集合的上下文中使用「item」。
NSIndexPath的核心接口在NSIndexPath.h中定義。索引的存儲位於_indexes中,它是NSUInteger的私有一維數組。 NSIndexPath本身可以表示任意數量的維度。 NSIndexPath上有兩個相關類別擴展了功能,一個來自UICollectionView.h「NSIndexPath(UICollectionViewAdditions)」,另一個來自UITableView.h「NSIndexPath(UITableView)」。 UICollectionView.h中的一個添加了只讀屬性「item」和相關的便捷方法。來自UITableView.h的添加了只讀屬性「row」和相關的便捷方法。然而,這兩個屬性只是訪問_indexes [1]中的基礎值的包裝器。
由於UIKit與兩個類別都鏈接,因此無論您在哪個IOS中使用它們,這兩組便利功能都始終可用。所以你可以從[NSIndexPath indexPathForRow:inSection:]創建一個NSIndexPath,但從indexPath.item中檢索第二個索引。無論由indexPath.item還是indexPath.row訪問,底層值都是完全相同的。
從文體上看,如果您將UICollectionView中的「item」和UITableView中的「row」一起使用,那麼它會更乾淨,因爲它們是如何使用它們的,這使得代碼更具可讀性。但是,如果你交換它們,你的程序不會崩潰。
參考:NSIndexPath
看UICollectionView.h的底部,你會看到,延伸NSIndexPath內的UICollectionView情況下使用時添加item
作爲屬性類別。
UITableView.h底部有一個類似的部分,它爲UITableViews中使用的NSIndexPath添加row
和section
屬性。
如果您試圖在類中訪問NSIndexPath實例的這些屬性,並且NSIndexPathInstance不相信它們存在,只需將定義它們的類的頭部導入到類的頂部,您就會神奇地能夠訪問這些屬性。
UICollectionView.h
@interface NSIndexPath (UICollectionViewAdditions)
+ (instancetype)indexPathForItem:(NSInteger)item inSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
@property (nonatomic, readonly) NSInteger item NS_AVAILABLE_IOS(6_0);
@end
UITableView.h
//_______________________________________________________________________________________________________________
// This category provides convenience methods to make it easier to use an NSIndexPath to represent a section and row
@interface NSIndexPath (UITableView)
+ (instancetype)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;
@property (nonatomic, readonly) NSInteger section;
@property (nonatomic, readonly) NSInteger row;
@end
要使用你的類中的這些屬性,你必須想要的一個導入到你的類像這樣:
@import "UIKit/UITableView.h"
然後你可以做事情lik e:myIndexPath.row
和[myIndexPath row]
是的,'行'由'UITableView'行使用; 'item'由'UICollectionView'單元使用。 – holex