2013-10-13 33 views
3

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

} 

回答

2

self.cellArray上執行就地洗牌以隨機排列其順序而不重複。我接過洗牌代碼this question,它應該放在一個NSMutableArray類別:

//CollectionViewController.m 

#import "NSMutableArray+Shuffle.h" 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.cellArray = @[@"Type 1", @"Type 2", @"Type 3", ...]; 

    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 
    NSMutableArray *arrayWithoutThirteenthObject = [NSMutableArray array]; 
    [arrayWithoutThirteenthObject addObjectsFromArray:[self.cellArray objectsAtIndexes:beforeThirteen]]; 
    [arrayWithoutThirteenthObject addObjectsFromArray:[self.cellArray objectsAtIndexes:afterThirteen]]; 

    //Shuffle it 
    [arrayWithoutThirteenthObject shuffle]; 

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

    //Assign the shuffled array to the table view array 
    self.cellArray = arrayWithoutThirteenthObject; 
} 

最後我做什麼是洗牌不第十三對象的數組,然後後重新添加它第十三指數這個數組已經被洗牌了,所以最後它被完全洗牌了,除了第十三個索引處的對象之外,它仍然保持在同一個地方。

只是爲了輔助功能的緣故,這裏的洗牌代碼:

//NSMutableArray+Shuffle.h 
@interface NSMutableArray (Shuffling) 
- (void)shuffle; 
@end 

//NSMutableArray+Shuffle.m 
@implementation NSMutableArray (Shuffling) 

- (void)shuffle { 
    NSUInteger count = [self count]; 
    for (NSUInteger i = 0; i < count; ++i) { 
     NSInteger nElements = count - i; 
     NSInteger n = (arc4random() % nElements) + i; 
     [self exchangeObjectAtIndex:i withObjectAtIndex:n]; 
    } 
} 

@end 
+0

我假設我要在創建NSArray + Shuffle.h和NSArray + Shuffle.m文件時使用子類「NSMutableArray」? – MacBoss123541

+0

*編輯上述評論* – MacBoss123541

+0

@RileyLloyd - 它不完全是一個子類;它被稱爲一個類別。它們是向現有類添加方法的一種方式。你必須進入Xcode,當你點擊「新文件」時,應該有一個選項來選擇「類別」。它會提示您輸入該類別的名稱;稱它爲「隨機播放」。 – pasawaya

0

集合是無序的,並不能包含重複所以他們(幾乎)你的問題的完美解決方案。我們首先從數組創建一個集合。

NSSet *set = [NSSet setWithArray:self.cellArray]; 

好偉大,但怎樣才能從這個無序組cooresponds到我們的索引路徑對象?我們只能使用anyObject從NSSet獲取對象。這可能會給我們兩次相同的對象。解決方案 - 製作一個有序集合。

NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithSet:set]; 

然後,我們可以從我們的集合中抽出一個無序但有序的字符串。

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