当前位置: 首页 > 知识库问答 >
问题:

在NSSet objective中插入索引路径-C

姬阳曜
2023-03-14

是否可以在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

共有1个答案

郎宏逸
2023-03-14

null

self.indexPathsOfSeenCells = [self.indexPathsOfSeenCells setByAddingObject:indexPath];

但是,理想情况下应该使用可变集,即

使用可变集,您可以简单地:

[self.indexPathsOfSeenCells addObject:indexPath];

还要注意,您的集合从未初始化,因此它始终是

你必须把:

self.indexPathsOfSeenCells = [NSMutableSet set];

某处。

 类似资料:
  • 问题内容: 在Xcode 6(Beta)中,有Swift编译器-搜索路径,导入路径。它有什么作用? 问题答案: 只是分享我在连接点的日子里发现的东西。 简短的答案 ,导入搜索路径指定Swift在何处查找和导入模块。 模块和语义导入 什么是模块?通过将文本预处理器包含模型替换为更健壮,更有效的语义模型,模块改善了对软件库API的访问。从用户的角度来看,该代码看起来仅稍有不同,因为一个代码使用了导入声

  • 问题内容: 我在RAD中收到以下错误: 您能否让我知道错误是什么以及如何解决? 问题答案: 索引16处有一个非法字符。我想说它不喜欢路径中的空格。您可以百分比编码特殊字符,例如空格。在这种情况下,将其替换为%20。 我上面链接的问题建议使用URLEncoder:

  • 问题内容: 我是Elasticsearch的新手,正在寻找使用Java API的帮助。我有一些领域对象,例如 我已经创建了连接到节点的传输客户端 有没有简单的方法将我的对象直接插入elasticsearch? 我看到了 但是要做到这一点,我必须将每个对象都转换成json,这虽然不是我理想的情况。 如果我对它的工作方式(架构上)有误解,请告诉我,我在这里学习! 欢呼声,罗布 问题答案: 我认为您在正

  • 问题内容: 说我有一个边列表,每个边包含两个节点(到和从)。找到两个给定节点的边缘的最佳方法是什么?请注意,边缘的节点可能会重复。 假设我在这种格式下具有优势: 1 <-> 5 3 <-> 7 5 <-> 6 2 <-> 6 然后,诸如1 5的查询将返回 true 。 然后,诸如5 2之类的查询将返回 true, 因为5连接6并且6连接至2。 然后,诸如1 7的查询将返回 false 。 然后,诸

  • 回到Jooq2.5,看起来可以通过FactoryOperations类设置PostgreSQL搜索路径,但Jooq3.5中没有该类。显然,FactoryOperations分为DSL和DSLContext,但我似乎找不到use(Schema)方法的结尾。如何在较新版本的jOOQ中设置PostgreSQL搜索路径?

  • 问题内容: 如何在另一个字符串的特定索引处插入一个字符串? 假设我想在“ foo”之后插入“ bar”,我该如何实现? 我想到了,但必须有一个更简单,更直接的方法。 问题答案: 您可以将自己的原型制作为String。 Polyfill 例 编辑: 对其进行了修改,以确保它是一个绝对值。