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"];

}
4

2 回答 2

2

执行就地洗牌self.cellArray以随机化其顺序而不重复。我从这个问题中获取了改组代码,它应该放在 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
于 2013-10-13T05:39:25.057 回答
0

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

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

好的,但是如何从这个无序集合中获取一个与我们的索引路径对应的对象呢?我们只能使用 NSSet 从 NSSet 中获取对象anyObject。这可以给我们两次相同的对象。解决方案 - 制作一个有序的集合。

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;
}
于 2013-10-13T06:01:18.257 回答