1.别的不多说,直接进入正题.先看看效果~
2.说说实现思想:首先,我自定义了一个imageVIew和自己的labelView,并添加了对应的index,以便于在点击视频进行播放的时候,能够依赖我们定义的index进行视频资源的搜索和加载.
代码如下:
1)LCImageViewController.h
//
// LCImageViewController.h
// tableViewCell添加ScrollView 实现动态滑动
//
// Created by lichan on 13-12-4.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface LCImageViewController : UIImageView
@property (assign,nonatomic)int imageIndexOfScrollView;
@end
2)LCImageViewController.m文件
//
// LCImageViewController.m
// tableViewCell添加ScrollView 实现动态滑动
//
// Created by lichan on 13-12-4.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import "LCImageViewController.h"
@implementation LCImageViewController
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
由于LCLabelViewController的.h和.m文件和image文件相同,不再赘述
3.LCViewController的实现,里面包含了 网络资源的加载(土豆channel中的网络加载已说明)和cell的动态绘制
先看看.h文件吧,我会在注释中解释意思
//
// LCViewController.h
// tableViewCell添加ScrollView 实现动态滑动
//
// Created by lichan on 13-12-4.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface LCViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,NSURLConnectionDataDelegate,NSURLConnectionDelegate>
@property (strong,nonatomic)UITableView *tableView; //表格
@property (strong,nonatomic)UITabBar *tabBar;
@property (strong,nonatomic)NSMutableData *resultData; //网络上接受的Data数据
@property (strong,nonatomic)NSMutableDictionary *resultDic;//data JSON解析成 Dic 数据
@property (strong,nonatomic)NSMutableArray *resultArray;//DIC 数据通过关键字得到的 Arr 数据
@property (strong,nonatomic)NSMutableDictionary *objectDic; //从resultArray中得到的 字典数据
@end
.m文件的实现
//
// LCViewController.m
// tableViewCell添加ScrollView 实现动态滑动
//
// Created by lichan on 13-12-4.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import "LCViewController.h"
#import "LCImageViewController.h"
#import "LCLabelViewController.h"
#define kNumberOfObjectsInSection 13
@interface LCViewController ()
@end
@implementation LCViewController
- (void)viewDidLoad
{
[super viewDidLoad];
/*
if (_resultData == nil) {
NSArray *pathDocs = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSAllDomainsMask, YES);
NSString *path = [[pathDocs objectAtIndex:0] stringByAppendingString:@"data.unHanle"];//数据的存储路径和名称
self.resultData = [NSMutableArray arrayWithContentsOfFile:path];
NSLog(@"%@---",_resultData);
}
[self createConnectionByChannelID:22];//选择频道,这里先设置成 99,等待后续接口
*/
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 440)];
[self.view addSubview:_tableView];
_tableView.delegate = self;
_tableView.dataSource = self;
_tabBar = [[UITabBar alloc]initWithFrame:CGRectMake(0, 440, 320, 40)];
_tabBar.backgroundColor = [UIColor yellowColor];
UIImage *image = [UIImage imageNamed:@"qq.png"];
UITabBarItem *tabBarItem = [[UITabBarItem alloc]initWithTitle:@"图片加载" image:image tag:1];
NSMutableArray *barItemArray = [[NSMutableArray alloc]initWithObjects:tabBarItem, nil];
_tabBar.items = barItemArray;
[self.view addSubview:_tabBar];
[self createConnectionByChannelID:22];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark UITableView Delegate method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.resultArray.count / 13 ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 110;
}
#pragma mark UITableViewData Delegate Method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = [indexPath row]; //关键点之一 cell的位置
self.objectDic = [self.resultArray objectAtIndex:1]; //关键点之二 数据的位置
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 110*row, 320, 110)];
scrollView.contentSize = CGSizeMake(1100, 110);
scrollView.tag = row +1;
static NSString *cellIdentifiter = @"Cellidentifiter";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifiter];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifiter];
//在scrollView 上面放置 13 个imageView,并设置imageIndex.
for (int i = 0; i < 13; i++) {
//------------------------图片的位置设置
CGFloat x = 85 * i;
CGFloat y = 0;
LCImageViewController *imageView = [[LCImageViewController alloc]initWithFrame:CGRectMake(x, y, 80, 80)];
imageView.imageIndexOfScrollView = i+1;//坐标位置
imageView.tag = i+1;
NSString *picURLString = [_objectDic objectForKey:@"picUrl"];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSData *data = [self loadImageAndMovieInfoByURL:picURLString];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
imageView.image = [UIImage imageWithData:data];
});
});
//------------------------label的位置设置
LCLabelViewController *labelView = [[LCLabelViewController alloc]initWithFrame:CGRectMake(x,80, 80, 20)];
labelView.labelIndexOfScrollView = i+1;
labelView.tag = i+1;
labelView.font = [UIFont boldSystemFontOfSize:10];
//label的 数据加载
//labelView.lineBreakMode = UILineBreakModeWordWrap;
labelView.numberOfLines = 2;
self.objectDic = [self.resultArray objectAtIndex:i];
labelView.text = [_objectDic objectForKey:@"title"];
[scrollView addSubview:imageView];
[scrollView addSubview:labelView]; //添加到 scrollview中
}
[cell.contentView addSubview:scrollView]; //添加到 cell 中
}
return cell;
}
#pragma mark 加载图片工具类
-(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;
}
#pragma mark 请求的组装和connection的组装
-(void)createConnectionByChannelID:(NSInteger)channelID
{
NSString *param = [[NSString alloc]initWithFormat:@"app_key=8f72aa77a6337080&channelId=%d&pageSize=13&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(@"连接失败");
}
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"%s",__func__);
}
#pragma mark NSURLConnectionDataDelegate Method
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.resultData = [NSMutableData dataWithCapacity:13];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.resultData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//存储或者不存储数据都行,存储数据是为了能够在缓存中使用
[self saveReceivedDataToFileByFileName:@"data.unHanle" WithData:self.resultData];
[self jsonDataParse];
}
#pragma mark JSON解析 --工具方法
- (void)jsonDataParse //ok
{
NSError *error;
// _resultDic = [NSJSONSerialization JSONObjectWithData:_resultData options:NSJSONWritingPrettyPrinted error:&error];
self.resultDic = [NSJSONSerialization JSONObjectWithData:self.resultData options:NSJSONWritingPrettyPrinted error:&error];
if (error != nil) {
NSLog(@"JSON解析发生了错误:%@",error);
}
//解析Dic数据,从中得到 视频的 results 信息 //还可以得到 page 信息
_resultArray = [_resultDic objectForKey:@"results"];
[self.tableView reloadData];
}
#pragma mark conneciton的 存储 --工具 method
- (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];
}
@end
4.解释说明:
由于我使用的接口是 土豆的测试API,所以每分钟的最大连接数为50/min.我苦于没办法加载太多的资源,所以只好加载一行作为演示了.但是整体来说,除了我们的播放没实现,其他的功能都使用了.再者,在image和label中加载手势也是很简单的事情,我在土豆channel中已经给出了代码,大家可以去看看.