gpt4 book ai didi

ios - 创建UICollectionView的最大单元数

转载 作者:行者123 更新时间:2023-12-01 17:55:08 29 4
gpt4 key购买 nike

在达到25个单元格后,如何使应用程序停止从阵列中创建单元格?就目前而言,数组“cellArray”包含25个以上的元素,并且没有代码可以阻止程序产生单元。

CollectionViewContoller.m:

    @implementation CollectionViewController

//Delegate Methods

-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}

-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.cellArray.count;
}

-(UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
Cell * aCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"bingoCell1" forIndexPath:indexPath];
aCell.cellContent.text = self.cellArray[indexPath.row];
return aCell;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

self.cellArray =
@[@"Type 1", @"Type 2", @"Type 3", @"Type 4", @"Type 5", @"Type 6", @"Type 7", @"Type 8", @"Type 9", @"Type 10", @"Type 11", @"Type 12", @"Free Space", @"Type 14", @"Type 15", @"Type 16", @"Type 17", @"Type 18", @"Type 19", @"Type 20", @"Type 21", @"Type 22", @"Type 23", @"Type 24", @"Type 25", @"Type 58", @"Type 234"];

NSIndexSet *beforeThirteen = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 12)];
NSIndexSet *afterThirteen = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(13, self.cellArray.count-13)];

//Build an array with all objects except for the thirteenth one and shuffle it
NSMutableArray *arrayWithoutThirteenthObject = [NSMutableArray array];
[arrayWithoutThirteenthObject addObjectsFromArray:[self.cellArray objectsAtIndexes:beforeThirteen]];
[arrayWithoutThirteenthObject addObjectsFromArray:[self.cellArray objectsAtIndexes:afterThirteen]];
[arrayWithoutThirteenthObject shuffle];

//Add the thirteenth object into the shuffled array
[arrayWithoutThirteenthObject insertObject:self.cellArray[12] atIndex:12];

//Assign the array now with the thirteenth object at the thirteenth index and shuffled
self.cellArray = arrayWithoutThirteenthObject;
}

最佳答案

如果您希望最大值为25,则可以执行以下操作:

-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return MIN(25, self.cellArray.count);
}

关于ios - 创建UICollectionView的最大单元数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19342488/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com