根据展现顺序,使用多线程加载后一个mkmapview对象,企图实现更友好的展现效果。我设想的,开始只需要显示一个tableview,而mapview是在后台不用立即显示出来。另外,如果加载到tableview的数据是来自网络,而不是测试中本地,那么也可以用多线程来实现。
我列举了两种多线程的使用方法,一种是NSOperationQueue,另一种是dispatch_async。两种方法孰优孰劣,我也不知道。对此有研究的网友请指点一二。
下面给出源码,如下所示:
#import <UIKit/UIKit.h>
@interface XYViewController : <UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *switchMapAndList;
- (IBAction)switchListOrMap:(id)sender;
@end
#import "XYViewController.h"
#import "XYTableView.h"
#import <MapKit/MapKit.h>
@interface XYViewController ()
{
XYTableView *resultList;
NSMutableArray *dataArray1;
NSMutableArray *dataArray2;
NSMutableArray *titleArray;
UIImageView *resultImg;
MKMapView *resultMap;
int listOrMap;
}
@end
@implementation XYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
resultList=[[XYTableView alloc] init];
[resultList setDelegate:self];
[resultList setDataSource:self];
[self.view addSubview:resultList];
dataArray1 = [[NSMutableArray alloc] initWithObjects:@"中国", @"美国", @"英国", nil];
dataArray2 = [[NSMutableArray alloc] initWithObjects:@"黄种人", @"黑种人", @"白种人", nil];
titleArray = [[NSMutableArray alloc] initWithObjects:@"国家", @"种族", nil];
listOrMap=0;
resultMap=[[MKMapView alloc] initWithFrame:CGRectMake(0, 50, 320, 410)];
/*
dispatch_queue_t qq=dispatch_queue_create("addsubview_queue",nil);
dispatch_async(qq,^(void){
[resultMap setAlpha:0.0];
[self.view addSubview:resultMap];
});
dispatch_release(qq);
*/
NSOperationQueue *queue=[[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^(void){
[resultMap setAlpha:0.0];
[self.view addSubview:resultMap];
}];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
switch (section) {
case 0:
return [titleArray objectAtIndex:section];//提取标题数组的元素用来显示标题
case 1:
return [titleArray objectAtIndex:section];//提取标题数组的元素用来显示标题
default:
return @"Unknown";
}
}
//指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [titleArray count];//返回标题数组中元素的个数来确定分区的个数
}
//指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
switch (section) {
case 0:
return [dataArray1 count];//每个分区通常对应不同的数组,返回其元素个数来确定分区的行数
break;
case 1:
return [dataArray2 count];
break;
default:
return 0;
break;
}
}
//绘制Cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
//初始化cell并指定其类型,也可自定义cell
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell=[[UITableViewCell alloc] initWithFrame:CGRectZero];
//cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
switch (indexPath.section) {
case 0://对应各自的分区
[[cell textLabel] setText:[dataArray1 objectAtIndex:indexPath.row]];//给cell添加数据
break;
case 1:
[[cell textLabel] setText:[dataArray2 objectAtIndex:indexPath.row]];
break;
default:
[[cell textLabel] setText:@"Unknown"];
}
return cell;//返回cell
}
- (IBAction)switchListOrMap:(id)sender {
self.switchMapAndList.title=listOrMap==0?@"list":@"map";
listOrMap=listOrMap==0?1:0;
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewContentModeBottom
animations:^{
[resultMap setAlpha:listOrMap];
}
completion:^(BOOL completed){
}];
}
@end
阅读完这段代码,眼睛累。那么,就养养眼吧。