| 
	
	
		
			
分类: IOS2014-06-30 20:33 471人阅读  
#import "MainViewController.h"  #import "Video.h"    #define kBaseURL @"http://192.168.3.252/~apple"    @interface MainViewController ()<UITableViewDataSource, UITableViewDelegate>  @property (strong, nonatomic) NSArray *dataList;  @property (weak, nonatomic) UITableView *tableView;  @end    @implementation MainViewController   
</pre><p class="p1">#pragma mark 实例化视图  - (void)loadView  {      self.view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];          CGRect frame = self.view.bounds;      UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height - 44) style:UITableViewStylePlain];          [tableView setDataSource:self];          [tableView setDelegate:self];          [tableView setRowHeight:80];      [self.view addSubview:tableView];      self.tableView = tableView;                UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, tableView.bounds.size.height, 320, 44)];      [self.view addSubview:toolBar];                UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithTitle:@"load json" style:UIBarButtonItemStyleDone target:self action:@selector(loadJson)];      UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithTitle:@"load xml" style:UIBarButtonItemStyleDone target:self action:@selector(loadXML)];      UIBarButtonItem *item3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];      [toolBar setItems:@[item3, item1, item3, item2, item3]];  }    #pragma mark -uitableview数据源方法  对于uitableview下面这两个方法是必须实现的。  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  {      return self.dataList.count;  }    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {          static NSString *ID = @"MyCell";      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];            if (cell == nil) {          cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];      }          Video *v = self.dataList[indexPath.row];            cell.textLabel.text = v.name;      cell.detailTextLabel.text = v.teacher;                      if (v.cacheImage == nil) {                  UIImage *image = [UIImage imageNamed:@"user_default.png"];          [cell.imageView setImage:image];                 [self loadImageAsyncWithIndexPath:indexPath];      }else      {          [cell.imageView setImage:v.cacheImage];      }                return cell;  }      #pragma mark 异步加载网络图片  - (void)loadImageAsyncWithIndexPath:(NSIndexPath *)indexPath  {      Video *v = self.dataList[indexPath.row];     NSString *imagePath = [NSString stringWithFormat:@"%@%@", kBaseURL, v.imageURL];      NSURL *imageUrl = [NSURL URLWithString:imagePath];                    NSURLRequest *request = [NSURLRequest requestWithURL:imageUrl];          [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {                                  v.cacheImage = [UIImage imageWithData:data];                  [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];      }];  }    #pragma mark 处理json数据  - (void)handlerJSONData:(NSData *)data  {                      NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];            NSLog(@"%@", array);                NSArray *docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);      NSString *path = [docs[0]stringByAppendingPathComponent:@"json.plist"];      [array writeToFile:path atomically:YES];               NSMutableArray *arrayM = [NSMutableArray array];      for (NSDictionary *dict in array) {          Video *video = [[Video alloc]init];                  [video setValuesForKeysWithDictionary:dict];          [arrayM addObject:video];      }      self.dataList = arrayM;          [self.tableView reloadData];            NSLog(@"%@", arrayM);  }    #pragma mark 加载json  - (void)loadJson  {      NSLog(@"load json");          NSString *str = @"http://www.baidu.com?format=json";  //这里是乱写的                      NSURL *url = [NSURL URLWithString:str];          NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:2.0f];          NSURLResponse *response = nil;      NSError *error = nil;          NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];          if (data != nil) {                  NSString *result = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];          NSLog(@"%@", result);                            [self handlerJSONData:data];      }else if (data == nil && error == nil){          NSLog(@"空数据");      }else      {          NSLog(@"%@", error.localizedDescription);      }  }  #pragma mark 加载xml  - (void)loadXML  {      NSLog(@"load xml");  }    @end     |