IOS非主线程更新界面tableview

本文由 @lonelyrains 出品,转载请注明出处。 
文章链接: http://blog.csdn.net/lonelyrains/article/details/9036789


<p><span style="font-family: Arial, Helvetica, sans-serif;">//</span></p>//  BIDDoubleComponentPickerViewController.m
//  Pickers
//
//  Created by minglz on 12-12-23.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ESConfigAndRunViewController.h"

@implementation ESConfigAndRunViewController

@synthesize ctrlTable;
@synthesize strTestList;
@synthesize btnTest;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.strTestList = [[NSMutableArray alloc]init];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.ctrlTable = nil;
    self.strTestList = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.strTestList count];
}

- (UITableViewCell *)tableView:(UITableView *)rhsTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SimpleTableIdentifier = @"SimlpeTableIdentifier";
    
    UITableViewCell *cell = [rhsTableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:SimpleTableIdentifier];
    }
    
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [self.strTestList objectAtIndex:row];
    
    return cell;
}

- (IBAction)OnBtnStart:(id)sender
{
    if(isStart == false)
    {
        [self.btnTest setTitle:@"停止自动测试" forState:nil];
        isStart = true;
        [self performSelectorInBackground:@selector(backgroundOperation:) withObject:nil];
    }
    else
    {
        isStart = false;
        [self.btnTest setTitle:@"启动自动测试" forState:nil];
    }
}

- (void)backgroundOperation:(id)unused
{
    @autoreleasepool
    {
        while(isStart)
        {
            [self.strTestList addObject:@"hello world"];
            [self performSelectorOnMainThread:@selector(updateWithResults:) withObject:nil waitUntilDone:NO];
            usleep(1000*1000);
        }
    }
}

- (void)updateWithResults:(NSArray*)theResults
{
    [ctrlTable reloadData];
}
@end
        本例程使用tableview显示测试结果,其实就是显示“hello world”。按“启动自动测试”按钮,开始测试;按钮变成“停止自动测试”,再点击按钮,变成“启动自动测试”,打印“hello world”停止。使用到 performSelectorInBackground 函数创建后台打印“hello world”的服务。使用performSelectorOnMainThread函数完成对主线程界面的更新。

你可能感兴趣的:(IOS非主线程更新界面tableview)