iOS开发网络篇—实现一个视频播放客户端小应用(一)

一、初步实现(完成tableview的基本数据展示)

1.前提说明

已经搭建了本地服务器,在本地服务器中存放了视频信息等资源。

服务器的资源

2.代码示例:

(1)新建一个项目,让主控制器继承自UItableviewControl

(2)根据资源的相关属性,创建数据模型

YYviodesModel.h文件

 1 //
2 // YYviodesModel.h
3 // 01-文顶顶客户端
4 //
5 // Created by apple on 14-6-29.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import <Foundation/Foundation.h>
10
11 @interface YYviodesModel : NSObject
12 /**
13 * 视频ID
14 */
15 @property(nonatomic,assign)int ID;
16 /**
17 * 视屏路径
18 */
19 @property(nonatomic,copy)NSString *url;
20 /**
21 * 视频名称
22 */
23 @property(nonatomic,copy)NSString *name;
24 /**
25 * 视频长度
26 */
27 @property(nonatomic,assign)int length;
28 /**
29 * 视频缩略图
30 */
31 @property(nonatomic,copy)NSString *image;
32
33 //提供一个对外的接口
34 +(instancetype)viodesModelWithDict:(NSDictionary *)dict;
35 @end

YYviodesModel.m文件

 1 //
2 // YYviodesModel.m
3 // 01-文顶顶客户端
4 //
5 // Created by apple on 14-6-29.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYviodesModel.h"
10
11 @implementation YYviodesModel
12 +(instancetype)viodesModelWithDict:(NSDictionary *)dict
13 {
14 YYviodesModel *model=[[YYviodesModel alloc]init];
15 //对象转换为int类型的
16 model.ID=[dict[@"id"] intValue];
17 model.url=dict[@"url"];
18 model.name=dict[@"name"];
19 model.length=[dict[@"length"] intValue];
20 model.image=dict[@"image"];
21
22 //不能使用KVC
23 // [model setValuesForKeysWithDictionary:dict];
24 return model;
25 }
26 @end

(3)在主控制器中,编写业务逻辑代码

YYViewController.m文件

  1 //
2 // YYViewController.m
3 // 01-文顶顶客户端
4 //
5 // Created by apple on 14-6-29.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYViewController.h"
10 #import "MBProgressHUD+MJ.h"
11 #import "YYviodesModel.h"
12 #import "UIImageView+WebCache.h"
13
14 @interface YYViewController ()
15 @property(nonatomic,strong)NSArray *videos;
16
17 @end
18
19 @implementation YYViewController
20
21 - (void)viewDidLoad
22 {
23 [super viewDidLoad];
24
25 [MBProgressHUD showMessage:@"正在努力加载中"];
26
27 //创建路径
28
29 NSString *urlStr=@"http://192.168.1.53:8080/MJServer/video";
30 NSURL *url=[NSURL URLWithString:urlStr];
31
32 //创建请求
33 NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];//默认为get请求
34 //设置最大的网络等待时间
35 request.timeoutInterval=5.0;
36
37 //获取主队列
38 NSOperationQueue *queue=[NSOperationQueue mainQueue];
39 //发起请求
40 [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
41 //隐藏HUD
42 [MBProgressHUD hideHUD];
43 if (data) {//如果请求成功,拿到服务器返回的数据
44 //解析拿到的数据(JSON方式)
45 NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
46 // NSArray *array=dict[@"video"];
47 NSArray *array=dict[@"videos"];
48
49 NSMutableArray *videos=[NSMutableArray array];
50 for (NSDictionary *dict in array) {
51 YYviodesModel *model=[YYviodesModel viodesModelWithDict:dict];
52 [videos addObject:model];
53 }
54 self.videos=videos;
55
56 //刷新表格
57 [self.tableView reloadData];
58
59 }else//如果请求失败,没有拿到数据
60 {
61 [MBProgressHUD showError:@"网络繁忙,等稍后再试!"];
62 }
63
64 }];
65 }
66
67 #pragma mark-数据源方法
68 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
69 {
70 return 1;
71 }
72 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
73 {
74 return self.videos.count;
75 }
76 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
77 {
78 static NSString *ID=@"ID";
79 UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
80 if (cell==nil) {
81 cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
82 }
83
84 //获取数据模型
85 YYviodesModel *model=self.videos[indexPath.row];
86 cell.textLabel.text=model.name;
87 NSString *length=[NSString stringWithFormat:@"时长%d分钟",model.length];
88 cell.detailTextLabel.text=length;
89
90 // video.image == resources/images/minion_01.png
91 NSString *imageUrl = [NSString stringWithFormat:@"http://192.168.1.53:8080/MJServer/%@", model.image];
92
93 //这里使用了第三方框架
94 [cell.imageView setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"placeholder"]];
95
96 return cell;
97 }
98
99 //设置cell的行高
100 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
101 {
102 return 70;
103 }
104 @end

说明:该项目中使用了第三方框架MBProgressHUD和SDWebImage。

模拟器情况:

二、调整cell的内部结构

提示:在下面的方法中设置cell中子控件如图片等的高度是不可行的。下面的方法值负责处理cell的数据,在返回cell之后,会调用系统的方法对cell的内部结构进行设置,如果在该方法中设置了frame,那么会被覆盖掉,是无效的。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{....}

实现思路:

(1)可以新建一个xib,使用xib设置。

(2)新建一个类,继承自UITablecell,在该类中的-(void)layoutSubviews中进行设置(注意:一定要先调用父类的layoutSubviews方法).

下面演示第二种做法:

在-(void)layoutSubviews中设置frame

 1 //
2 // YYCell.m
3 // 01-文顶顶客户端
4 //
5 // Created by apple on 14-6-29.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYCell.h"
10
11 @interface YYCell ()
12 @property(nonatomic,weak)UIView * divider;
13 @end
14 @implementation YYCell
15
16 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
17 {
18 self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
19 if (self) {
20
21 //加一条线
22 UIView *divider=[[UIView alloc]init];
23 [divider setBackgroundColor:[UIColor brownColor]];
24 divider.alpha=0.5;
25 [self.contentView addSubview:divider];
26 self.divider=divider;
27
28 }
29 return self;
30 }
31
32 -(void)layoutSubviews
33 {
34 [super layoutSubviews];
35
36 //调整frame
37 //图片的frame
38 CGFloat imageX=10;
39 CGFloat imageY=10;
40 CGFloat imageH=self.frame.size.height-2*imageY;
41 CGFloat imageW=imageH*200/112;
42 self.imageView.frame=CGRectMake(imageX, imageY, imageW, imageH);
43
44 //标题的frame
45 CGRect textF=self.textLabel.frame;
46 textF.origin.x=imageW+2*imageX;
47 self.textLabel.frame=textF;
48
49 //小标题的frame
50 CGRect detailTextF=self.detailTextLabel.frame;
51 detailTextF.origin.x=textF.origin.x;
52 self.detailTextLabel.frame=detailTextF;
53
54 //设置下划线的frame
55 CGFloat dividerH=1.0;
56 CGFloat dividerW=self.frame.size.width;
57 CGFloat dividerY=self.frame.size.height-1;
58 self.divider.frame=CGRectMake(0, dividerY, dividerW, dividerH);
59 }
60
61 @end

主控制器文件代码:

  1 //
2 // YYViewController.m
3 // 01-文顶顶客户端
4 //
5 // Created by apple on 14-6-29.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYViewController.h"
10 #import "MBProgressHUD+MJ.h"
11 #import "YYviodesModel.h"
12 #import "UIImageView+WebCache.h"
13 #import "YYCell.h"
14
15 @interface YYViewController ()
16 @property(nonatomic,strong)NSArray *videos;
17
18 @end
19
20 @implementation YYViewController
21
22 - (void)viewDidLoad
23 {
24 [super viewDidLoad];
25 //去掉下划线
26 self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
27
28 [MBProgressHUD showMessage:@"正在努力加载中"];
29
30 //创建路径
31
32 NSString *urlStr=@"http://192.168.1.53:8080/MJServer/video";
33 NSURL *url=[NSURL URLWithString:urlStr];
34
35 //创建请求
36 NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];//默认为get请求
37 //设置最大的网络等待时间
38 request.timeoutInterval=20.0;
39
40 //获取主队列
41 NSOperationQueue *queue=[NSOperationQueue mainQueue];
42 //发起请求
43 [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
44 //隐藏HUD
45 [MBProgressHUD hideHUD];
46 if (data) {//如果请求成功,拿到服务器返回的数据
47 //解析拿到的数据(JSON方式)
48 NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
49 // NSArray *array=dict[@"video"];
50 NSArray *array=dict[@"videos"];
51
52 NSMutableArray *videos=[NSMutableArray array];
53 for (NSDictionary *dict in array) {
54 YYviodesModel *model=[YYviodesModel viodesModelWithDict:dict];
55 [videos addObject:model];
56 }
57 self.videos=videos;
58
59 //刷新表格
60 [self.tableView reloadData];
61
62 }else//如果请求失败,没有拿到数据
63 {
64 [MBProgressHUD showError:@"网络繁忙,等稍后再试!"];
65 }
66
67 }];
68 }
69
70 #pragma mark-数据源方法
71 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
72 {
73 return 1;
74 }
75 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
76 {
77 return self.videos.count;
78 }
79 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
80 {
81 static NSString *ID=@"ID";
82 YYCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
83 if (cell==nil) {
84 cell=[[YYCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
85 }
86
87 //获取数据模型
88 YYviodesModel *model=self.videos[indexPath.row];
89 cell.textLabel.text=model.name;
90 NSString *length=[NSString stringWithFormat:@"时长%d分钟",model.length];
91 cell.detailTextLabel.text=length;
92
93 // video.image == resources/images/minion_01.png
94 NSString *imageUrl = [NSString stringWithFormat:@"http://192.168.1.53:8080/MJServer/%@", model.image];
95
96 //这里使用了第三方框架
97 [cell.imageView setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"placeholder"]];
98
99 return cell;
100 }
101
102 //设置cell的行高
103 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
104 {
105 return 70;
106 }
107 @end

模拟器运行效果:

iOS开发网络篇—实现一个视频播放客户端小应用(一)的更多相关文章

  1. iOS开发UI篇—实现一个私人通讯录小应用【转】

    转一篇学习segue不错的教程 一.该部分主要完成内容 1.界面搭建                        2.功能说明 (1).只有当账号和密码输入框都有值的时候,登录按钮才能交互 (2). ...

  2. iOS开发网络篇—大文件的多线程断点下载

    http://www.cnblogs.com/wendingding/p/3947550.html iOS开发网络篇—多线程断点下载 说明:本文介绍多线程断点下载.项目中使用了苹果自带的类,实现了同时 ...

  3. iOS开发网络篇—网络编程基础

    iOS开发网络篇—网络编程基础 一.为什么要学习网络编程 1.简单说明 在移动互联网时代,移动应用的特征有: (1)几乎所有应用都需要用到网络,比如QQ.微博.网易新闻.优酷.百度地图 (2)只有通过 ...

  4. iOS开发网络篇—HTTP协议

    iOS开发网络篇—HTTP协议 说明:apache tomcat服务器必须占用8080端口 一.URL 1.基本介绍 URL的全称是Uniform Resource Locator(统一资源定位符) ...

  5. iOS开发网络篇—NSURLConnection基本使用

    iOS开发网络篇—NSURLConnection基本使用 一.NSURLConnection的常用类 (1)NSURL:请求地址 (2)NSURLRequest:封装一个请求,保存发给服务器的全部数据 ...

  6. 02.iOS开发网络篇—HTTP协议

    iOS开发网络篇—HTTP协议 说明:apache tomcat服务器必须占用8080端口 一.URL 1.基本介绍 URL的全称是Uniform Resource Locator(统一资源定位符) ...

  7. iOS开发网络篇—大文件的多线程断点下载(转)

    http://www.cnblogs.com/wendingding/p/3947550.html   iOS开发网络篇—多线程断点下载 说明:本文介绍多线程断点下载.项目中使用了苹果自带的类,实现了 ...

  8. iOS开发网络篇—多线程断点下载

    iOS开发网络篇—多线程断点下载 说明:本文介绍多线程断点下载.项目中使用了苹果自带的类,实现了同时开启多条线程下载一个较大的文件.因为实现过程较为复杂,所以下面贴出完整的代码. 实现思路:下载开始, ...

  9. iOS开发网络篇—JSON数据的解析

    iOS开发网络篇—JSON数据的解析 iOS开发网络篇—JSON介绍 一.什么是JSON JSON是一种轻量级的数据格式,一般用于数据交互 服务器返回给客户端的数据,一般都是JSON格式或者XML格式 ...

  10. iOS开发网络篇—XML数据的解析

     iOS开发网络篇—XML数据的解析 iOS开发网络篇—XML介绍 一.XML简单介绍 XML:全称是Extensible Markup Language,译作“可扩展标记语言” 跟JSON一样,也是 ...

随机推荐

  1. java基础之object类、Date类、System类、StringBuilder类、包装类、枚举类

    一.public String toString() :默认返回该对象的字符串表示,其实该字符串内容就是对象的类型+@+内存地址值 重写后: @Override public String toStr ...

  2. mybatis-plus之配置安全

    1. 环境 SpringBoot 2.6.x 2. 介绍 MyBatis-Plus 从3.3.2版本开始提供了数据安全保护功能,MyBatis-Plus 支持通过加密配置来增强数据库的安全性. 3. ...

  3. oracle的order by 中文排序原理

    近期发现oracle的order by中文排序并不是完全按照拼音排序的 经过测试发现oracle的order by中文排序是按照中文的ASCII码排序的 查询字符ASCII码 select ascii ...

  4. eolinker响应预处理:传参解决方法(截取返回数据中的某一段数据,正则截取)

    特别注意:需要使用全局变量或者预处理前务必阅读本链接https://www.cnblogs.com/becks/p/13713278.html 场景描述: 登录用例A,参加活动用例B,用户参加活动需要 ...

  5. eolinker请求预处理:请求数据中有中文,提示参数错误的解决方法

    特别注意:需要使用全局变量或者预处理前务必阅读本链接https://www.cnblogs.com/becks/p/13713278.html 如下图,请求参pageName参数为中文,提交后报错 需 ...

  6. 康谋方案 | 基于AI自适应迭代的边缘场景探索方案

    构建巨量的驾驶场景时,测试ADAS和AD系统面临着巨大挑战,如传统的实验设计(Design of Experiments, DoE)方法难以有效覆盖识别驾驶边缘场景案例,但这些边缘案例恰恰是进一步提升 ...

  7. 手把手带你从论文出发实战搭建分割FCN网络

    作者:SkyXZ CSDN:SkyXZ--CSDN博客 博客园:SkyXZ - 博客园 FCN论文地址:Fully Convolutional Networks for Semantic Segmen ...

  8. 对List进行分页工具类

    对List<?>进行分页工具类 package com.soft.mpms.zfream.util; import java.util.List; /** * @ClassName 对Li ...

  9. eclipse中那些难以分辨的符号、Java中的Long和mysql中的bigint

    这是一个字符串 从左到右依次是数字"0", 大写字母"O",小写字母"o",数字"1",小写字母"l(艾欧)& ...

  10. 鸿蒙 NEXT (一)初识鸿蒙

    @charset "UTF-8"; .markdown-body { line-height: 1.75; font-weight: 400; font-size: 15px; o ...