現在,下面的代碼從數組「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