Randomize records in a table view
Randomize records in a table view
- Subject: Randomize records in a table view
- From: "Bobby B" <email@hidden>
- Date: Fri, 14 Apr 2006 16:47:33 -0400
Hey guys;
Ok, so I have a set of data from CoreData hooked via bindings into a
TableView. I'm trying to find a way to randomize the rows in the
table view. This is the best I could figure.
It seems to work. This goes through the tabe and finds out how many
rows there are, then makes an array that size, and then initializes
the array with all NOs (an array of booleans). It picks a random
number and checks the position in the array to see if it's been used
already (= YES), if it hasn't, it sets the row # corresponding to the
random number to be that random number index, and then makes the spott
as taken in the array.
The only thing I didn't show is at the end a SortDescriptors based on "random".
This works.. But it's totally inelegant. Any suggestions? :-)
Thank you
Bobby B
- (IBAction) ranomizeRows:(id)sender {
// the srandom(time(NULL)); is in my awakeFromNib!
int maximumSelection = [[imageArray
valueForKeyPath:@"arrangedObjects.@count"] intValue];
BOOL randomArray[maximumSelection];
BOOL arrayIsFull = NO;
int randomIndex = 0;
int count = 0;
for (count = 0; count < maximumSelection; count++) {
randomArray[count] = NO;
}
while (arrayIsFull == NO) {
randomIndex = (random() % maximumSelection);
if (randomArray[randomIndex] == NO) {
randomArray[randomIndex] = YES;
[imageArray setSelectionIndex: randomIndex];
[imageArray setValue: [NSNumber numberWithInt:randomIndex]
forKeyPath:@"selection.random"];
}
int yesCount = 0;
for (count = 0; count < maximumSelection; count++) {
if (randomArray[count] == YES) {yesCount++;}
}
if (yesCount == count) {arrayIsFull = YES;}
}
[self saveAction: self];
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden