[新手学IOS]项目实战-土豆客户端(2): searchViewController的实现
由于searchvViewController 和 channel的实现很类似,直接贴了代码吧
1.
//
// LCSearchViewController.h
// 中北土豆客户端
//
// Created by lichan on 13-11-30.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//#import <UIKit/UIKit.h>@interface LCSearchViewController : UIViewController<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate,NSURLConnectionDataDelegate>@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;@property (weak, nonatomic) IBOutlet UITableView *tableView;@property (strong, nonatomic) IBOutletCollection(UITabBarItem) NSArray *doSearch;@property (copy,nonatomic)NSString *searchName;@property (strong,nonatomic)NSMutableData *searchResultData;@property (strong,nonatomic)NSMutableDictionary *handledDataDic;@property (strong,nonatomic)NSMutableArray *resultDataArr;
@property (strong,nonatomic)NSMutableArray *pageDataArr;
@property (strong,nonatomic) NSMutableDictionary * objectsDic;- (IBAction)backgroundTap:(id)sender;@end
2..m文件
//
// LCSearchViewController.m
// 中北土豆客户端
//
// Created by lichan on 13-11-30.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import "LCSearchViewController.h"
#import "LCCellController.h"
#import "LCPlayViewController.h"
@interface LCSearchViewController ()
@end
@implementation LCSearchViewController
- (IBAction)backgroundTap:(id)sender
{
[self.searchBar resignFirstResponder];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchBar.delegate =self;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark searchBar method
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
self.searchName = searchText;
if ([self.searchName isEqualToString:@""]) {
[self viewDidLoad];
[self.searchBar resignFirstResponder];
}
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.searchBar resignFirstResponder];
NSString *param = [NSString stringWithFormat:@"app_key=59337596b4aeb64c&format=json&kw=%@&pageNo=1&pageSize=20",self.searchName];
NSLog(@"param: %@",param);
NSURL *url = [[NSURL alloc] initWithString:@"http://api.tudou.com/v6/video/search"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[param dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *conn=[NSURLConnection connectionWithRequest:request delegate:self];
if (conn)
NSLog(@"连接成功");
else
NSLog(@"连接失败");
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[self.searchBar resignFirstResponder];
[self viewDidLoad];
}
#pragma mark URLConnectionData method
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"开始接受数据");
self.searchResultData = [NSMutableData dataWithCapacity:20];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.searchResultData appendData:data];
}
#pragma mark URLConnectionDelegate
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSArray *docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES);
NSString *path = [docs objectAtIndex:0];
path = [path stringByAppendingString:@"search.json"];
NSLog(@"文件路径 %@",path);
[self.searchResultData writeToFile:path atomically:YES];
[self jsonDataParse];
}
- (void) jsonDataParse
{
NSError *error;
self.handledDataDic = [NSJSONSerialization JSONObjectWithData:self.searchResultData options:NSJSONWritingPrettyPrinted error:&error];
if (error != nil) {
NSLog(@"%@",error);
}
self.resultDataArr = [self.handledDataDic objectForKey:@"results"];
[self saveReceivedDataToFileByFileName:@"result" WithData:self.resultDataArr];
NSLog(@"=========数据保存已完成");
NSLog(@"resultDataArr: %@",self.resultDataArr);
self.pageDataArr = [self.handledDataDic objectForKey:@"page" ];
[self.tableView reloadData];//重新加载数据.
NSLog(@"重新加载数据");
}
- (void)saveReceivedDataToFileByFileName:(NSString *)fileName WithData:(id)data //???? 什么样的文件写入?
{
NSArray *pathDocs = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSAllDomainsMask, YES);
NSString *path = [[pathDocs objectAtIndex:0] stringByAppendingString:[NSString stringWithFormat:@"%@",fileName]];//数据的存储路径和名称
[data writeToFile:path atomically:YES];
}
#pragma mark tableView Method
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.resultDataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 104.0;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"\"%@\"搜索结果:",self.searchName];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIndentifier = @"cellIndentifiter";
static BOOL nibsRegistered = NO;
if (!nibsRegistered) {
UINib *nib = [UINib nibWithNibName:@"LCCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:CellIndentifier];
nibsRegistered = YES;
}
LCCellController *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
NSInteger row = [indexPath row];
for (int i = 0; i < 3; i++) {
//实现多线程加载数据
self.objectsDic = [self.resultDataArr objectAtIndex:row *3 +i ];
NSString *objectTitle = [self.objectsDic objectForKey:@"title"];
if(i+1 == 1)
{
cell.label1.text = objectTitle;
cell.imageView1.indexURL = row*3+i+1;
}
else if(i+1 == 2)
{
cell.label2.text = objectTitle;
cell.imageView2.indexURL = row*3+i+2;
}
else if(i+1 == 3)
{
cell.label3.text = objectTitle;
cell.imageView3.indexURL = row*3+i+3;
}
//图片的加载
NSString *picUrlString = [self.objectsDic objectForKey:@"picUrl"];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
__block UIImage *infoimage;//为了保存blcok处理的值
dispatch_group_t group = dispatch_group_create();
//多线程1,实现图片加载
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
NSData *imageData = [self loadImageAndMovieInfoByURL:picUrlString];
infoimage = [UIImage imageWithData:imageData scale:1.14];
});
//多线程2:实现label 加载
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
});
//后续可以增加更多的线程进行多线程效率下载
//
//只有其他进程执行完毕才会执行这个block.//暂时为空
dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
dispatch_sync(dispatch_get_main_queue(), ^{
if(i+1 == 1)
{
cell.imageView1.image = infoimage;
cell.imageView1.backgroundColor = [UIColor orangeColor];
cell.imageView1.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(play:)];
[cell.imageView1 addGestureRecognizer:tap];
NSLog(@"添加了tap事件");
}
else if(i+1 == 2)
{
cell.imageView2.image = infoimage;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(play:)];
cell.imageView2.backgroundColor = [UIColor orangeColor];
cell.imageView2.userInteractionEnabled = YES;
[cell.imageView2 addGestureRecognizer:tap];
}
else if(i+1 == 3)
{
cell.imageView3.image = infoimage;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(play:)];
cell.imageView3.backgroundColor = [UIColor orangeColor];
cell.imageView3.userInteractionEnabled = YES;
[cell.imageView3 addGestureRecognizer:tap];
}
//回归主线程
});
//
});
});
}
return cell;
}
#pragma 工具类
- (IBAction)searchBarDoneEditing:(id)sender
{
[sender resignFirstResponder];
}
//对图片和label信息进行多线程加载
-(NSData *)loadImageAndMovieInfoByURL:(NSString *)urlString
{
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20];
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
return data;
}
-(void)play:(UITapGestureRecognizer *)tap
{
NSLog(@"调用了 play事件");
LCMyImageView *imageView = (LCMyImageView *)tap.view;
LCPlayViewController *playController = [[LCPlayViewController alloc]init];
playController.searcher = self;
playController.playURL = [NSString stringWithFormat:@"http://www.tudou.com/programs/view/html5embed.action?code=%@",[[self.resultDataArr objectAtIndex:imageView.indexURL] objectForKey:@"itemCode"]];
NSLog(@",playController.playURL :%@",playController.playURL);
[self presentViewController:playController animated:YES completion:nil];
}
@end
[新手学IOS]项目实战-土豆客户端(2): searchViewController的实现相关推荐
- [新手学IOS]第十天-土豆客户端:加载土豆API,解析JSON填充自定义TableViewCell数据(channle的重点实现)(12618)
好久不见,我已经两天没有发布关于IOS的文章了,不知道大家有没有想念呢?嘿嘿,我知道很少人在看,但是没关系,我不会气馁的.以后我会更加努力的写好,我知道自己前面的文章中有些说的不知所然,我会努力的,一 ...
- Swift开发iOS项目实战视频教程(一)---iOS真简单
本课主要介绍iOS项目的创建.第一个iOS项目的开发.UILabel.UIButton的使用. 假设你看完此视频还认为iOS非常难,请你来找我! 本教程摒弃枯燥的语法和知识解说,全是有趣有料的项目实战 ...
- ASP.NET Core分布式项目实战(客户端集成IdentityServer)--学习笔记
任务9:客户端集成IdentityServer 新建 API 项目 dotnet new webapi --name ClientCredentialApi 控制器添加验证 using Microso ...
- 1个G带你学软件测试项目实战教程:《学车不》APP测试
APP测试不会做?软件测试不熟练?别着急,今天就帮助大家走出这个困境,分享一套APP测试项目教程,视频有1个G的压缩包,内容挺多,建议跟随课程,看起来! 课程内容: 1. APP测试基础知识(架构.发 ...
- react-native 项目实战 -- 新闻客户端(6) -- 完善ListView头部视图
1.因为需要定时器,所以我们要cd到当前项目根目录下安装这个类库: $ npm i react-timer-mixin --save 2.Component/ScrollImage.js /*** 滚 ...
- 2019最新Python从入门到精通之30天快速学Python项目实战(完整)
课程目录 python入门教程-1-Python编程语言历史及特性.mkv python入门教程-2-Python编程语言初接触.mkv python入门教程-3-Python程序文件结构.mkv ...
- ASP.NET Core分布式项目实战(详解oauth2授权码流程)--学习笔记
最近公司产品上线,通宵加班了一个月,一直没有更新,今天开始恢复,每日一更,冲冲冲 任务13:详解oauth2授权码流程 我们即将开发的产品有一个用户 API,一个项目服务 API,每个服务都需要认证授 ...
- ASP.NET Core分布式项目实战(集成ASP.NETCore Identity)--学习笔记
任务24:集成ASP.NETCore Identity 之前在 Index 页面写了一个 strong 标签,需要加个判断再显示,不然为空没有错误的时候也会显示 @if (!ViewContext.M ...
- ASP.NET Core分布式项目实战(Consent 确认逻辑实现)--学习笔记
任务22:Consent 确认逻辑实现 接下来,我们会在上一节的基础上添加两个按钮,同意和不同意,点击之后会把请求 post 到 ConsentController 处理,如果同意会通过 return ...
最新文章
- 机器人建模中移动关节如何建立坐标系_机器人工程师进阶之路(八)指数积(PoE)建立机械臂模型及正运动...
- C#基础笔记(第九天)
- Python---进阶---logging---logger
- oracle知识汇总
- varnish 缓存php,Varnish Cache不会使用会话缓存PHP除非后端TTL被更改
- 计算机解决科学研究,近期查处科研不端行为典型案例及处理决定
- 给图片加一层半透明_设计半透明风格卡通星球图片的PS教程
- 大剑无锋之hadoop默认的数据类型都有哪些?
- 看动画学算法系列之:后缀树suffix tree
- PHP中的加强型接口Traits
- 达内TTS6.0课件oop_day01
- 你初吻啥时候没有的​?​
- 长脸了!阿里云这位英雄拿下了世界第一
- 一个不限制插值个数和上采样倍数的视频增强方法
- leetcode题库--112路径总和
- 带ant 的收发器_ANT无线收发器nRF24AP1及其应用
- 2023年山东大学社会工作考研成功上岸经验分享
- Ubuntu系统下如何提交代码到GitHub
- 动手下载网易课程视频 -- 正式下载
- android 打开摄像头
热门文章
- C语言中const与指针
- 无人值守的性能测试 -- for 淘宝技术嘉年华 TCon2011
- B+树相比B树的优势
- skype安卓手机版_安卓手机QQ 8.4.10版发布:来 开个视频包厢
- 单挂/组队·单/双哈比·法师/狼猎·开源
- matlab仿真Gz转换为差分方程,怎么用matlab把传递函数转成差分方程
- 微软为何要收购诺基亚?
- photoshop位图与智能对象、矢量图形区别
- 操作系统原理学习笔记(文件与I/O)
- 小程序影藏溢出的gif_如何在Android手机上快速实现视频转GIF动图呢?