2013-02-08 14 views
51

有沒有人知道NSIndexpath.rowNSIndexpath.item之間的區別?NSIndexpath.item vs NSIndexpath.row

具體說,一個做我使用:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
+1

是的,'行'由'UITableView'行使用; 'item'由'UICollectionView'單元使用。 – holex

回答

122

好了,沒有人在這裏賜給一個很好的答案。

在NSIndexPath中,索引存儲在一個名爲「_indexes」的簡單c數組中,定義爲NSUInteger *,數組長度存儲在定義爲NSUInteger的「_length」中。訪問器「section」是「_indexes [0]」的別名,「item」和「row」都是「_indexes [1]」的別名。因此這兩者在功能上是相同的。

在編程風格方面 - 也許是定義鏈 - 您最好在表格的上下文中使用「row」,在集合的上下文中使用「item」。

+18

太棒了!所以實際上沒有任何區別。 – fatuhoku

+0

我喜歡簡單的回答而不是解釋它是如何工作的等等。好工作 – Codenator81

+0

人們對此的瞭解如何打擊我的想法,只是我一直在尋找的答案!非常感謝! – Dan

9

您需要使用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的細節

+2

當你從UICollectionView {Delegate,DataSource}'訪問'row'時,或者如果你從'UITableView {Delegate,DataSource}'中訪問'item',會發生什麼? – fatuhoku

+0

你得到一個訪問衝突或更糟 – iosengineer

+5

它仍然會工作 – Lirik

25
indexPath.row is best in your case 

約NSIndexPath

一是信息的NSIndexPath類表示嵌套數組的樹路徑的特定節點集合。這條路徑被稱爲索引路徑。

indexPath中的每個索引都表示從樹中的一個節點到另一個較深節點的子數組的索引。

例如,indexPath 1.4.3。2指定你的情況indexPath.row返回該行的在該特定indexPath索引圖 enter image description here

這裏所示的路徑。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. 
+0

你可能剛剛給了NSIndexPath類參考鏈接,你知道嗎? – esh

+19

也許,但通常建議在答案中包含相關部分。鑑於其他答案給出的回答太窄,我很欣賞指出更廣泛使用'NSIndexPath'的人。 – Rob

+4

我仍然看不到indexPath。項目,而問題提出的其中一個問題是indexPath.item和indexPath.row之間的區別。 Midhun議員的答案就是這樣。沒有一定數量的闡述,複製粘貼文檔就沒有多大幫助。 – esh

1

@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

0

看UICollectionView.h的底部,你會看到,延伸NSIndexPath內的UICollectionView情況下使用時添加item作爲屬性類別。

UITableView.h底部有一個類似的部分,它爲UITableViews中使用的NSIndexPath添加rowsection屬性。

如果您試圖在類中訪問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]

相關問題