dsfsdffsd

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@property (nonatomic,strong) ViewController *viewController;
@end


@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.viewController = [[ViewController alloc] initWithNibName:nil bundle:nil];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = self.viewController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

@end


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>


@end

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,strong) NSMutableArray *randomNumbers;
@property (nonatomic,strong) UITableView *myTableView;
@end

@implementation ViewController

-(NSString *) fileLocation{
    NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    if ([folders count] == 0) {
        return nil;
    }
    NSString *documentsFolder = [folders objectAtIndex:0];
    return [documentsFolder stringByAppendingPathComponent:@"list.txt"];
}

- (BOOL)hasFileAlreadyBeenCreated{
    BOOL result = NO;
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    if ([fileManager fileExistsAtPath:[self fileLocation]]) {
        result = YES;
    }
    return result;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(concurrentQueue, ^{
        NSUInteger numberOfValuesRequired = 10000;
        if ([self hasFileAlreadyBeenCreated] == NO) {
            dispatch_sync(concurrentQueue, ^{
                NSMutableArray *arrayOfRandomNumbers = [[NSMutableArray alloc] initWithCapacity:numberOfValuesRequired];
                NSUInteger counter = 0;
                for (counter = 0; counter < numberOfValuesRequired; counter++) {
                    unsigned int randomNumber = arc4random() %((unsigned int)RAND_MAX + 1);
                    [arrayOfRandomNumbers addObject:[NSNumber numberWithUnsignedInt:randomNumber]];
                }
                [arrayOfRandomNumbers writeToFile:[self fileLocation] atomically:YES];
            });
        }
        
        dispatch_sync(concurrentQueue, ^{
            if ([self hasFileAlreadyBeenCreated]) {
                self.randomNumbers = [[NSMutableArray alloc] initWithContentsOfFile:[self fileLocation]];
                [self.randomNumbers sortUsingComparator:^NSComparisonResult(id obj1,id obj2){
                    NSNumber *number1 = (NSNumber *)obj1;
                    NSNumber *number2 = (NSNumber *)obj2;
                    return [number1 compare:number2];
                }];
            }
        });
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            if ([self.randomNumbers count] > 0) {
                self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
                self.myTableView.delegate = self;
                self.myTableView.dataSource = self;
                self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
                [self.view addSubview:self.myTableView];
            }
        });
    });
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.randomNumbers.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *result = nil;
    static NSString *TableViewCellIdentifer = @"MyCells";
    result = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifer];
    if (result == nil) {
        result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCellIdentifer];
        result.textLabel.text = [NSString stringWithFormat:@"%d",self.randomNumbers[indexPath.row]];
    }
    return result;
}

@end

 

posted @ 2015-07-30 10:59  小艾利  阅读(324)  评论(0编辑  收藏  举报