好久不见,我已经两天没有发布关于IOS的文章了,不知道大家有没有想念呢?嘿嘿,我知道很少人在看,但是没关系,我不会气馁的.以后我会更加努力的写好,我知道自己前面的文章中有些说的不知所然,我会努力的,一定.~
好了,现在开始说说我想实现的土豆客户端吧.先截图,给大家共享下图片吧~
1/图片
2.由于我今天就实现了这一个模块,我就先说这一个,随后一个一个补充,好么?嘿嘿
首先,我定义了一个自定义的cell,相信大家都看的出来.我先贴出来代码,然后上个图.
.h
//
// LCCellController.h
// 中北土豆客户端
//
// Created by lichan on 13-12-1.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import <UIKit/UIKit.h>
#define kLabelTag 1
@interface LCCellController : UITableViewCell
@property (strong, nonatomic) IBOutlet UIImageView *imageView1;
@property (strong, nonatomic) IBOutlet UIImageView *imageView2;
@property (strong, nonatomic) IBOutlet UIImageView *imageView3;
@property (strong, nonatomic) IBOutlet UILabel *label1;
@property (strong, nonatomic) IBOutlet UILabel *label2;
@property (strong, nonatomic) IBOutlet UILabel *label3;
@end
.m文件没什么内容, 就不贴了
xib文件.虽然使用了storeBoard,但是我还是使用了xib文件建立了cell,你不会怪我吧
2.然后看看我们的channelViewController 文件.虽然里面有很多的属性没有用,但是我会用得,别着急哦
如果我没用,我就在后面备注了一个?.
//
// LCChannelViewController.h
// 中北土豆客户端
//
// Created by lichan on 13-11-30.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface LCChannelViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,NSURLConnectionDataDelegate,NSURLConnectionDelegate>
@property UIView *channelView ;
@property (strong, nonatomic) IBOutlet UILabel *channelTextShow;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property(strong,nonatomic)NSMutableData *unHandleData; //接收数据
@property(strong,nonatomic)NSMutableData *handledData;//?
@property (strong,nonatomic)NSMutableDictionary *objectsDic;//?
@property (strong,nonatomic)NSMutableDictionary *handledDataDic;//从data json转化的dic数据
@property (strong,nonatomic)NSMutableArray *resultDataArr;
@property (strong,nonatomic)NSMutableArray *pageDataArr;
@property (strong,nonatomic)NSMutableArray *unHandleDataArr;//?
@property (strong,nonatomic)NSMutableArray *handleDataArr;//?
@property UIButton *moiveButton,*musicButton,*hotButton,*cartoonButton,*tvplayButton,*varShowButton;
-(void)createChannelView;
- (IBAction)channelPressed:(id)sender;
@end
里面实现了四个协议,分别是 tableViewDelegate,dataDelegate,ConnectionDelegate,ConnectionDataDelegate.这四个协议都是必须实现的协议哦.我们会在.m文件中详细的实现.
ch.m文件,里面可能有些乱,一些接口的使用比较多,但是我都分好块了哦~~
//
// LCChannelViewController.m
// 中北土豆客户端
//
// Created by lichan on 13-11-30.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import "LCChannelViewController.h"
#import "LCCellController.h"
#define kCellRowObject 3
#define kCellColumn 1
@interface LCChannelViewController ()
@end
@implementation LCChannelViewController
@synthesize myTableView;
@synthesize channelView,channelTextShow;
@synthesize moiveButton,musicButton,hotButton,cartoonButton,tvplayButton,varShowButton;
@synthesize unHandleDataArr,handleDataArr;
@synthesize objectsDic,handledDataDic;//定义字典接收数据
@synthesize resultDataArr,pageDataArr;
//可以实现图片和文字结合,实现tableview
-(void)createChannelView//可以实现更多的左右上下滑动~~~显示
{//这是点击 三角符号的时候出现的小菜单,我们创建了6个button.每个都添加了选择器,
if (channelView == nil) {
channelView = [[UIView alloc]initWithFrame:CGRectMake(0, 57, 320, 100)];
channelView.backgroundColor = [UIColor grayColor];
moiveButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80,40 )];
[moiveButton setTitle:@"电影" forState:UIControlStateNormal];
[moiveButton setTag:22];
[moiveButton addTarget:self action:@selector(channelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
musicButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 0, 80, 40)];
[musicButton setTitle:@"音乐" forState:UIControlStateNormal];
[musicButton setTag:14];
[musicButton addTarget:self action:@selector(channelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
hotButton = [[UIButton alloc]initWithFrame:CGRectMake(200, 0, 80, 40)];
[hotButton setTitle:@"热点" forState:UIControlStateNormal];
[hotButton setTag:29];
[hotButton addTarget:self action:@selector(channelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
cartoonButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 50, 80, 40)];
[cartoonButton setTitle:@"动漫" forState:UIControlStateNormal];
[cartoonButton setTag:9];
[cartoonButton addTarget:self action:@selector(channelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
tvplayButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 50, 80, 40)];
[tvplayButton setTitle:@"电视剧" forState:UIControlStateNormal];
[tvplayButton setTag:30];
[tvplayButton addTarget:self action:@selector(channelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
varShowButton = [[UIButton alloc]initWithFrame:CGRectMake(200, 50, 80, 40)];
[varShowButton setTitle:@"综艺" forState:UIControlStateNormal];
[varShowButton setTag:31];
[varShowButton addTarget:self action:@selector(channelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[channelView addSubview:moiveButton];
[channelView addSubview:musicButton];
[channelView addSubview:hotButton];
[channelView addSubview:cartoonButton];
[channelView addSubview:tvplayButton];
[channelView addSubview:varShowButton];
}
[self.view addSubview:channelView];//把这个小菜单加到view中,但是要设置成隐藏.
}
//channleButton!!//
-(void)channelButtonPressed:(id)sender
{
self.channelView.hidden = YES;
int buttonTag = [sender tag];
UIButton *button = (UIButton *)[sender viewWithTag:buttonTag];
channelTextShow.text = button.titleLabel.text;
[self createConnectionByChannelID:buttonTag];
NSLog(@"点击了%d",buttonTag);
}
//点击按钮或者label 显示. button!!//这里的bug是第一次使用的时候,必须点击两次,我还没找到原因,可以帮我看看么
- (IBAction)channelPressed:(id)sender {
//pressTime++;
[self createChannelView];
NSLog(@"hidden:%d",self.channelView.hidden);
if (self.channelView.hidden == YES)
{
self.channelView.hidden = NO;
NSLog(@"no");
return;
}
else
{
self.channelView.hidden = YES;
NSLog(@"yes");
return;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
//如果我们不判断这句话,那么我们首次启动的时候 数据位nil,那么我们的tableview是空白的,很可怕哦
if (resultDataArr == nil) {
NSArray *pathDocs = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSAllDomainsMask, YES);
NSString *path = [[pathDocs objectAtIndex:0] stringByAppendingString:@"result"];//数据的存储路径和名称
self.resultDataArr = [NSMutableArray arrayWithContentsOfFile:path];
}
self.channelView.hidden = YES;//我发现我初始化了 隐藏,但是到了去判断的时候,发现还是no,真捉急~
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
[self createConnectionByChannelID:10];
//self.handleDataArr = [[NSMutableArray alloc]initWithObjects:@"1",@"11",@"111",@"12",@"122",@"1222",@"13",@"133",@"1333",@"14",@"144",@"1444",@"15",nil];
// Do any additional setup after loading the view.
}
//----------------------------------------------------------
//tableview date delegate的协议实现
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger col = (NSInteger) [self.resultDataArr count]/kCellRowObject;
return col;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return kCellColumn;
}
-(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 < kCellRowObject; i++) {
//实现多线程加载数据
self.objectsDic = [self.resultDataArr objectAtIndex:row *3 +i ];
NSString *objectTitle = [objectsDic objectForKey:@"title"];
if(i+1 == 1)
{
cell.label1.text = objectTitle;
}
else if(i+1 == 2)
{
cell.label2.text = objectTitle;
}
else if(i+1 == 3)
{
cell.label3.text = objectTitle;
}
//图片的加载
NSString *picUrlString = [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;
}
else if(i+1 == 2)
{
cell.imageView2.image = infoimage;
}
else if(i+1 == 3)
{
cell.imageView3.image = infoimage;
}
//回归主线程
});
//
});
});
}
return cell;
}
//对图片和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;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 104.0;
}
//--------------------------文件写入------------------------
//文件写入接口
-(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];
}
//-------------------------连接--------------------------
// 创建接口,实现请求连接
-(void)createConnectionByChannelID:(NSInteger)channelID
{
NSString *param = [[NSString alloc]initWithFormat:@"app_key=59337596b4aeb64c&format=json&channelId=%d&pageNo=1&pageSize=20&orderBy=t",channelID];
NSURL *url = [NSURL URLWithString:@"http://api.tudou.com/v6/video/top_list"];
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(@"连接失败");
}
//connection协议的实现
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"----------------返回的数据类型是%@",[response textEncodingName]);
self.unHandleData = [NSMutableData dataWithCapacity:20];
//定容量.
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"开始接收数据,数据包大小: %d",[data length]);
[self.unHandleData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[self saveReceivedDataToFileByFileName:@"channle.JSON" WithData:self.unHandleData];
[self jsonDataParse];
}
//使用原生态的appleAPI.返回视频的数组信息
-(void) jsonDataParse
{
NSError *error;
self.handledDataDic = [NSJSONSerialization JSONObjectWithData:self.unHandleData options:NSJSONWritingPrettyPrinted error:&error];
if (error != nil) {
NSLog(@"%@",error);
}
self.resultDataArr = [self.handledDataDic objectForKey:@"results"];
[self saveReceivedDataToFileByFileName:@"result" WithData:self.resultDataArr];
NSLog(@"=========数据保存已完成");
self.pageDataArr = [self.handledDataDic objectForKey:@"page" ];
[self.myTableView reloadData];//重新加载数据.
NSLog(@"重新加载数据");
}
//-----------------------------------------
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
@end