现在,下面的代码从数组“cellArray”中抽取数据以形成一个包含25个单元格的UICollectionView。我想随机化正在使用的数据而不重复。截至目前,只有25个数组元素。稍后,会有更多,但我只想要出现25个单元格。我尝试插入“NSUInteger randomIndex = arc4random()%[theArray count];”但是,我无法让它工作,并且从我的理解来看,这存在模数偏差。如何从数组中随机化单元格内容
如果第13个单元格可以有恒定(不变)的文本,这也是一个好处。
@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"];
}
我假设我要在创建NSArray + Shuffle.h和NSArray + Shuffle.m文件时使用子类“NSMutableArray”? – MacBoss123541
*编辑上述评论* – MacBoss123541
@RileyLloyd - 它不完全是一个子类;它被称为一个类别。它们是向现有类添加方法的一种方式。你必须进入Xcode,当你点击“新文件”时,应该有一个选项来选择“类别”。它会提示您输入该类别的名称;称它为“随机播放”。 – pasawaya