在UITableView中,我可以在.h文件中看到这些:
@interface NSIndexPath (UITableView)
+ (NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;
@property(nonatomic,readonly) NSInteger section;
@property(nonatomic,readonly) NSInteger row;
@end
在UICollectionView中,我可以找到以下类别:
@interface NSIndexPath (UICollectionViewAdditions)
+ (NSIndexPath *)indexPathForItem:(NSInteger)item inSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
@property (nonatomic, readonly) NSInteger item NS_AVAILABLE_IOS(6_0);
@end
但是,如果在 collectionView 的委托类中,我在 collectionView 的 indexPath 中引用"section"属性,Xcode 不会给出任何警告,为什么?
-(NSInteger)collectionView:(UICollectionView*)cv numberOfItemsInSection:(NSInteger)section {
if (cv == self.collectionView) {
if (cv.collectionViewLayout == self.layout2) {
return [self.searches count];
} else {
NSString *searchTerm = self.searches[section];
return [self.searchResults[searchTerm] count];
}
} else if (cv == self.currentPinchCollectionView) {
//commend for below == @property (nonatomic, strong) NSIndexPath *currentPinchedItem;
NSString *searchTerm = self.searches[self.currentPinchedItem.section];// this "section" is NOT for tableView, why NO warning/error from Xcode?
return [self.searchResults[searchTerm] count];
}
return 0;
}
我的猜测是 IndexPath 对象是在 UITableView.h 中创建的。然后,将项属性添加到 UICollectionView.h 中的 IndexPath 类别中,因为行属性在集合视图中没有任何意义。但是,section 仍然是框架中任何地方的可用属性,因为这是索引路径的重点。