Why doesn't the table reset?


#1

When I build and run the current project in the simulator the same objects in the table show up every time. Even though I choose “Reset content and settings” in the iOS Simulator. I have tested with the downloadable code as well.

When the app is uninstalled in the simulator I have a hard time figuring out where the previous memory of what objects the table contains should be hidden.

In the init method of ItemsViewController we create 10 random BNRItems and insert them in allItems using the method createItem in BNRItemStore.

ItemsViewController.m

- (id)init 
{
    // Call the superclass's designated initializer
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        for (int i = 0; i < 10; i++) {
            [[BNRItemStore defaultStore] createItem];
        }
    }
    return self;
}

BNRItemStore.m

- (BNRItem *)createItem
{
    BNRItem *p = [BNRItem randomItem];

    [allItems addObject:p];
   
    return p;
}

So, how do I reset the table view and get to se some other items in the table?


#2

Are you trying to say that your random items are not random at all every time you run the program?

Then, you need to change the way you generate random numbers. Instead of using rand () or random (), use the function arc4random () (or arc4random_uniform (u_int32_t)).

#include <stdio.h>
#include <stdlib.h>

int main ()
{
   const u_int32_t U = 32;
   printf ("%u\n", arc4random_uniform (U));
   printf ("%u\n", arc4random_uniform (U));
   printf ("%u\n", arc4random_uniform (U));
   return 0;
}

#3

Great, now it works when I changed from rand() to arc4random(). Thanks!