NSIndexpath.item 대 NSIndexpath.row
사람 사이의 차이를 알고 있나요 NSIndexpath.row
과 NSIndexpath.item
?
구체적으로 어떤 것을 사용합니까?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
좋아요, 아무도 여기서 좋은 답을주지 않았습니다.
NSIndexPath 내에서 인덱스는 NSUInteger *로 정의 된 "_indexes"라는 간단한 c 배열에 저장되고 배열의 길이는 NSUInteger로 정의 된 "_length"에 저장됩니다. 접근 자 "section"은 "_indexes [0]"의 별칭이고 "item"과 "row"는 모두 "_indexes [1]"의 별칭입니다. 따라서 두 기능은 기능적으로 동일합니다.
프로그래밍 스타일과 정의 체인 측면에서 테이블 컨텍스트에서는 "행"을 사용하고 컬렉션 컨텍스트에서는 "항목"을 사용하는 것이 좋습니다.
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- indexPath에서 UITableView
특정 행 기반 을 얻기 위해 속성 사용 . 또한 읽기 전용 속성입니다.
Available in iOS 2.0 and later.
항목- 섹션에서 항목UICollectionView
을 가져 오기 위해와 함께 올바르게 사용 합니다. 읽기 전용 속성입니다. 이 속성을 사용하려면 UICollectionView.h 에서 선언해야합니다.
> Available in iOS 6.0 and later.
당신은 사용해야합니다 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 추가 사항을 확인 하십시오.
@Owen Godfrey's answer is better than the accepted answer from @iPatel. Here is some further clarification which I wasn't able to fit into a comment on his answer, so I'll copy his answer and add to it here. Credit belongs to Owen.
From @Owen Godfrey:
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 "_indexes1". 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.
The core interface of NSIndexPath is defined in NSIndexPath.h. The storage of the indexes is in _indexes which is a private one-dimensional array of NSUInteger. NSIndexPath by itself can represent any number of dimensions. There are two relevant categories on NSIndexPath which extend the functionality, one from UICollectionView.h "NSIndexPath (UICollectionViewAdditions)" and one from UITableView.h "NSIndexPath (UITableView)". The one from UICollectionView.h adds the readonly property "item" and related convenience methods. The one from UITableView.h adds the readonly property "row" and related convenience methods. However both properties are just wrappers that access the underlying value in _indexes[1].
Since UIKit links with both categories, both sets of convenience functions are always available, no matter where in IOS you are using them. So you could create an NSIndexPath from [NSIndexPath indexPathForRow:inSection:] but retrieve the second index from indexPath.item. The underlying value is exactly the same whether accessed by indexPath.item or indexPath.row.
Stylistically it is cleaner if you use "item" with UICollectionView and "row" with UITableView as that is how they were intended to be used, and this makes for more readable code. However your program won't crash if you interchange them.
Reference: NSIndexPath
Look at the bottom of UICollectionView.h and you will see the category that extends NSIndexPath to add item
as a property when used within for UICollectionView instances.
UITableView.h 하단에는 UITableViews 에서 사용되는 NSIndexPaths에 대한 속성 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"
그리고 당신은 일을 같이 할 수있다 : myIndexPath.row
및[myIndexPath row]
참고 URL : https://stackoverflow.com/questions/14765730/nsindexpath-item-vs-nsindexpath-row
'development' 카테고리의 다른 글
Google Maps API V3에서 fitBounds ()를 사용한 후 setZoom () 사용 (0) | 2020.10.25 |
---|---|
Python에서 구분 된 줄에 목록 요소 인쇄 (0) | 2020.10.25 |
비교 기준으로 findBy 메소드를 사용하는 방법 (0) | 2020.10.25 |
파이썬 정규식이 모든 겹치는 일치를 찾으십니까? (0) | 2020.10.25 |
Java에서 두 문자열 세트를 결합하는 더 좋은 방법이 있습니까? (0) | 2020.10.25 |