加UITableView 到自定義的UIViewController的操作 在自定義的視圖控制器類中,通過代碼添加uitableview對(duì)象和mkmapview對(duì)象,,實(shí)現(xiàn)兩個(gè)視圖對(duì)象的展現(xiàn)切換,。tableview的delegate和datasource也在這個(gè)自定義的視圖控制器類中實(shí)現(xiàn)。這樣就不用依賴于XCODE提供的uitableviewcontroller類,。這樣更加靈活,,有利于開發(fā)出更強(qiáng)大的應(yīng)用。 根據(jù)展現(xiàn)順序,,使用多線程加載后一個(gè)mkmapview對(duì)象,,企圖實(shí)現(xiàn)更友好的展現(xiàn)效果。我設(shè)想的,,開始只需要顯示一個(gè)tableview,,而mapview是在后臺(tái)不用立即顯示出來。另外,,如果加載到tableview的數(shù)據(jù)是來自網(wǎng)絡(luò),,而不是測(cè)試中本地,那么也可以用多線程來實(shí)現(xiàn),。 我列舉了兩種多線程的使用方法,,一種是NSOperationQueue,另一種是dispatch_async。兩種方法孰優(yōu)孰劣,,我也不知道,。對(duì)此有研究的網(wǎng)友請(qǐng)指點(diǎn)一二。
下面給出源碼,,如下所示:
#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:@"中國(guó)", @"美國(guó)", @"英國(guó)", nil]; dataArray2 = [[NSMutableArray alloc] initWithObjects:@"黃種人", @"黑種人", @"白種人", nil]; titleArray = [[NSMutableArray alloc] initWithObjects:@"國(guó)家", @"種族", 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];//提取標(biāo)題數(shù)組的元素用來顯示標(biāo)題 case 1: return [titleArray objectAtIndex:section];//提取標(biāo)題數(shù)組的元素用來顯示標(biāo)題 default: return @"Unknown"; } }
//指定有多少個(gè)分區(qū)(Section),,默認(rèn)為1 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [titleArray count];//返回標(biāo)題數(shù)組中元素的個(gè)數(shù)來確定分區(qū)的個(gè)數(shù) } //指定每個(gè)分區(qū)中有多少行,默認(rèn)為1 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ switch (section) { case 0: return [dataArray1 count];//每個(gè)分區(qū)通常對(duì)應(yīng)不同的數(shù)組,,返回其元素個(gè)數(shù)來確定分區(qū)的行數(shù) 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://對(duì)應(yīng)各自的分區(qū) [[cell textLabel] setText:[dataArray1 objectAtIndex:indexPath.row]];//給cell添加數(shù)據(jù) 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
|