如何随机化数组中的单元格内容

时间:2013-10-13 05:28:45

标签: ios objective-c arrays nsarray xcode5

按照目前的情况,下面的代码从数组“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 个答案:

答案 0 :(得分: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;
}

最后我做的是在没有第十三个对象的情况下对数组进行洗牌,然后在数组被洗牌之后将其添加回第十三个索引,所以最后它完全被洗牌,除了第十三个索引处的对象,它一直在同一个地方。

为了便于访问,这里是shuffle的代码:

//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

答案 1 :(得分:0)

集合是无序的,不能包含重复项,因此它们(几乎)是您问题的完美解决方案。让我们从你的数组创建一个集合开始。

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

好的,但是如何从这个无序集中获取一个对象到我们的索引路径?我们只能使用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;
}