基于phpmysql的网站开发国外设计大师网站
web/
2025/10/6 17:11:08/
文章来源:
基于phpmysql的网站开发,国外设计大师网站,太湖云建站网站建设,宁波的网络营销服务公司app store中的很多应用程序非常的笨重#xff0c;他们有好的界面#xff0c;但操作性很差#xff0c;比如说当程序从网上或本地载入数据的时候#xff0c;界面被冻结了#xff0c;用户只能等程序完全载入数据之后才能进行操作。当打开一个应用程序时#xff0c;iphone会产…app store中的很多应用程序非常的笨重他们有好的界面但操作性很差比如说当程序从网上或本地载入数据的时候界面被冻结了用户只能等程序完全载入数据之后才能进行操作。当打开一个应用程序时iphone会产生一个包含main方法的线程所用程序中的界面都是运行在这个线程之中的table views, tab bars, alerts…,有时候我们会用数据填充这些view现在问题是如何有效的载入数据并且用户还能自如的操作程序。下面要说方法的并不是要在用户载入数据的时候在界面上提示“loading”的信息虽然这种方式在有些时候是可以被接受的但当数据在main线程之外被载入是并不是最有效的方式。先看一下要演示的程序这个程序将从网络上下载10,000条数据并填入到UITableView中现面的代码将首先演示一种错误的方式错误 (源码 )
implementationRootViewController
synthesizearray;-(void)viewDidLoad {[super viewDidLoad];/* Adding the button */self.navigationItem.rightBarButtonItem [[UIBarButtonItem alloc]initWithTitle:Loadstyle:UIBarButtonItemStyleDone
target:selfaction:selector(loadData)];/* Initialize our array */NSMutableArray*_array [[NSMutableArrayalloc]initWithCapacity:10000];
self.array_array;
[_array release];
}// Fires when the user presses the load button-(void)loadData {/* Grab web data */NSURL*dataURL [NSURLURLWithString:http://icodeblog.com/samples/nsoperation/data.plist];NSArray*tmp_array [NSArrayarrayWithContentsOfURL:dataURL];/* Populate our array with the web data */for(NSString*str intmp_array){[self.arrayaddObject:str];
}/* reload the table */[self.tableViewreloadData];
}#pragma mark Table view methods-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return1;
}-(NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section {return[self.arraycount];
}-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath {staticNSString*CellIdentifier Cell;UITableViewCell *cell[tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if(cellnil){cell[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier]autorelease];
}/* Display the text of the array */[cell.textLabel setText:[self.arrayobjectAtIndex:indexPath.row]];returncell;
}-(void)dealloc{[super dealloc];
[arrayrelease];
}end 当点击“load”按钮时程序会被冻结直到将数据完全下载并填入Tableview在这期间用户不能做任何的事情。在给出解决方式之前先来看一下NSOperationQueue和NSOperation The NSOperation and NSOperationQueue classes alleviate much of the pain of multi-threading, allowing you to simply define your tasks, set any dependencies that exist, and fire them off. Each task, or operation , is represented by an instance of an NSOperation class; the NSOperationQueueclass takes care of starting the operations, ensuring that they are run in the appropriate order, and accounting for any priorities that have been set. 下面要做的是建立NSOperationQueue和NSOperations。NSOperationQueue会建立一个线程每个加入到线程operation会有序的执行。下面是使用NSOperationQueue的过程 建立一个NSOperationQueue的对象建立一个NSOperation的对象将operation加入到NSOperationQueue中release掉operation 使用NSOperation有几种,现在介绍最简单的一种NSInvocationOperationNSInvocationOperation是NSOperation的子类允许运行在operation中的targer和selector。 下面是执行NSInvocationOperation的一个例子
NSOperationQueue*queue [NSOperationQueuenew];NSInvocationOperation*operation[[NSInvocationOperationalloc]initWithTarget:selfselector:selector(methodToCall)object:objectToPassToMethod];[queue addOperation:operation];
[operationrelease]; 下面是我们用正确的方式实现的程序正确的方式(下载源码 )
implementationRootViewController
synthesizearray;-(void)viewDidLoad {[super viewDidLoad];self.navigationItem.rightBarButtonItem [[UIBarButtonItem alloc]initWithTitle:Loadstyle:UIBarButtonItemStyleDonetarget:selfaction:selector(loadData)];NSMutableArray*_array [[NSMutableArrayalloc]initWithCapacity:10000];self.array_array;[_array release];
}-(void)loadData {/* Operation Queue init (autorelease) */NSOperationQueue*queue [NSOperationQueuenew];/* Create our NSInvocationOperation to call loadDataWithOperation, passing in nil */NSInvocationOperation*operation[[NSInvocationOperationalloc]initWithTarget:selfselector:selector(loadDataWithOperation)object:nil];/* Add the operation to the queue */[queue addOperation:operation];[operationrelease];
}-(void)loadDataWithOperation {NSURL*dataURL [NSURLURLWithString:http://icodeblog.com/samples/nsoperation/data.plist];NSArray*tmp_array [NSArrayarrayWithContentsOfURL:dataURL];for(NSString*str intmp_array){[self.arrayaddObject:str];}[self.tableViewreloadData];
}#pragma mark Table view methods-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return1;
}-(NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section {return[self.arraycount];
}-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath {staticNSString*CellIdentifier Cell;UITableViewCell *cell[tableViewdequeueReusableCellWithIdentifier:CellIdentifier];if(cellnil){cell[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];}[cell.textLabel setText:[self.arrayobjectAtIndex:indexPath.row]];returncell;
}-(void)dealloc{[super dealloc];[arrayrelease];
} 再次运行程序当点击“load”按钮时界面是否还被“冻结”呢程序并没有增加很多的代码但确大大的提高了用户体验。转载于:https://www.cnblogs.com/zhwl/archive/2013/01/05/2845281.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/88024.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!