2013-10-13 34 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; 
}