NSIndexPath.row VS NSIndexPath.item
in 技术 with 2 comments

NSIndexPath.row VS NSIndexPath.item

in 技术 with 2 comments

前言

这是一篇翻译文,帮你彻底理解 NSIndexPath.row 和 NSIndexPath.item 的区别。

解答

以下是原英文答案:

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.

In terms of programming style – and perhaps the definition chain – you would be better using "row" in the context of tables, and "item" in the context of collections.

翻译文:

在 NSIndexPath 内部,索引是存储在一个名为 _indexes 的简单的 C 语言数组中,类型为 NSUInteger* 类型。这个数组的长度是存储在名叫 _length 的数组中,类型为 NSUInteger。

访问器 .section 是 _indexes[0] 的别名,访问器 .item 和 .row 是 _indexes[1] 的别名。因此,这两者在功能上是相同的。

**在编程风格方面 - 也许是其定义链 - 你最好在 tableview 的上下文中使用 .row,在 collectionview 的上下文中使用 .item。**

原 StackOverflow 答案

Responses / Cancel Reply
  1. Kornan

    写得很好,简单易懂!

    Reply
    1. @Kornan

      多谢夸奖

      Reply