問題
NSIndexpath屬性item和row的根本區別是什麼?
分析
Inside NSIndexPath, the indexes are stored in a simple c array called “_indexes” defined as NSUInteger* and the length of the array is stored in “_length” defined as NSUInteger. The accessor “section” is an alias to “_indexes[0]” and both “item” and “row” are aliases to “_indexes[1]”. Thus the two are functionally identical.
翻譯過來就是, 在NSIndexPath
中,其實例中存儲在一個名爲_indexes
的簡單的c數組中,定義爲NSUInteger *
,數組的長度存儲在定義爲NSUInteger
的_length
中。 屬性section
是_indexes[0]
的別名,屬性item
和row
都是_indexes[1]
的別名。 因此兩者在功能上是相同的。
總結
雖然item
和row
在功能上是一致的, 但是從編碼風格上來說, 並且結合上下文環境來說, tableview
中一般使用row
會更好些, collectionview
中一般使用item
會更好些。
想了解更多, 看這裏,參考資料:
NSIndexpath.item vs NSIndexpath.row