是否可以在NSSET中插入索引路径值?基本上,我试图将indexPath插入到我的NSSet中,NSSet是我的CollectionView中所有旋转的GreenCells的集合,但是由于某种原因,它没有工作:\这是我的代码:ViewController.m
编辑:我添加了所有的代码,使它更容易。
#import "ViewController.h"
//#import "CollectionViewTest-Swift.h"
#import "CustomCollectionViewCell.h"
#import "AppDelegate.h"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, ImpressionStalkerDelegate, UIScrollViewDelegate, UICollectionViewDelegateFlowLayout>
@property NSArray *indexPathsOfCellsTurnedGreen_Arr;
@property NSMutableSet *indexPathsOfSeenCells;
@property ImpressionStalker *impressionEventStalker;
@end
@implementation ViewController
{
}
@synthesize Collection_view;
- (void)viewDidLoad {
[super viewDidLoad];
self.impressionEventStalker = [[ImpressionStalker alloc] initWithMinimumPercentageOfCell:0.70 collectionView:Collection_view delegate:self];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.myViewController = self;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// [impressionEventStalker stalkCells];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 100;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL_ID" forIndexPath:indexPath] ;
// cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];
[cell configure:[NSString stringWithFormat:@"%ld", indexPath.row] configure: NO];
// cell.cellBackground.backgroundColor = [UIColor redColor];
// if ([_indexPathsOfCellsTurnedGreen_Arr containsObject:indexPath]) {
// cell.cellBackground.backgroundColor = [UIColor greenColor];
// } else {
// cell.cellBackground.backgroundColor = [UIColor redColor];
//
// }
if ([_indexPathsOfSeenCells containsObject:indexPath]) {
cell.cellBackground.backgroundColor = [UIColor greenColor];
} else {
cell.cellBackground.backgroundColor = [UIColor redColor];
}
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake( [ [UIScreen mainScreen]bounds ].size.width - 40, 325);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 20, 0, 20);
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
[(CustomCollectionViewCell*)cell startTimer:^{
[self.impressionEventStalker stalkCells];
}];
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
[(CustomCollectionViewCell*)cell stopTimer];
}
- (void)sendEventForCellAtIndexPath:(NSIndexPath * _Nonnull)indexPath {
CustomCollectionViewCell *cell = (CustomCollectionViewCell*)[Collection_view cellForItemAtIndexPath:indexPath];
cell.cellBackground.backgroundColor = [UIColor greenColor];
// [_indexPathsOfCellsTurnedGreen_Arr arrayByAddingObject:indexPath];
// [_indexPathsOfSeenCells setByAddingObject:indexPath];
[_indexPathsOfSeenCells setByAddingObject:indexPath];
NSLog(@"%lu",(unsigned long)_indexPathsOfSeenCells.count);
}
// A func which sends all the seen cells to the server:.
- (NSSet*)sendStalkDataToServer {
return _indexPathsOfSeenCells;
}
@end
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UICollectionView *Collection_view;
- (NSSet*)sendStalkDataToServer;
@end
null
self.indexPathsOfSeenCells = [self.indexPathsOfSeenCells setByAddingObject:indexPath];
但是,理想情况下应该使用可变集,即
使用可变集,您可以简单地:
[self.indexPathsOfSeenCells addObject:indexPath];
还要注意,您的集合从未初始化,因此它始终是
你必须把:
self.indexPathsOfSeenCells = [NSMutableSet set];
某处。