iOS Development Web chapter-Implementing a video playback client small application (i)

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/wendingding/p/3815052.html

First, the initial realization (complete TableView basic data show)

1. Prerequisite Instructions

A local server has been built, and resources such as video information are stored on the local server.

Resources for the server

2. code example:

(1) Create a new project to inherit the host controller from Uitableviewcontrol

(2) Create a data model based on the relevant attributes of the resource

YYviodesModel.h file

1//2//  YYviodesModel.h 3//  01-Text top-top client 4//5//  Created by Apple on 14-6-29.6/  Copyright (c) 2014 I Tcase. All rights reserved. 7//8  9 #import <foundation/foundation.h>10 @interface yyviodesmodel:nsobject12/**13  *  video id14< C7/>*/15 @property (nonatomic,assign) int id;16/**17  *  video path  */19 @property (nonatomic,copy) NSString * Url;20/**21  *  video name  */23 @property (nonatomic,copy) nsstring *name;24/**25  *  Video Length 26  */27 @property (nonatomic,assign) int length;28/**29  *  video thumbnail  */31 @property (nonatomic,copy) NSString  *image;32 33//provide an external interface (INSTANCETYPE) viodesmodelwithdict: (nsdictionary *) dict;35 @end

YYVIODESMODEL.M file

1//2//  YYVIODESMODEL.M 3//  01-Text top-top client 4//5//  Created by Apple on 14-6-29.6/  Copyright (c) 2014 I Tcase. All rights reserved. 7//8  9 #import "YYviodesModel.h" @implementation YYviodesModel12 + (instancetype) Viodesmodelwithdict: ( Nsdictionary *) dict13 {     Yyviodesmodel *model=[[yyviodesmodel alloc]init];15     //object converted to type int     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     //cannot use KVC23//    [model Setvaluesforkeyswithdictionary:dict];24     return model;25}26 @end

(3) in the host controller, write the business logic code

YYVIEWCONTROLLER.M file

  1//2//YYVIEWCONTROLLER.M 3//01-Text top-top client 4//5//Created by Apple on 14-6-29. 6//Copyright (c) 2014 itcase.  All rights reserved. 7//8 9 #import "YYViewController.h" #import "mbprogresshud+mj.h" #import "YYviodesModel.h" #import "Uiimagev Iew+webcache.h "@interface Yyviewcontroller () @property (Nonatomic,strong) Nsarray *videos;      @end @implementation Yyviewcontroller-(void) viewdidload [[Super Viewdidload]; 24 25 [Mbprogresshud showmessage:@ "is trying to load in"]; 26 27//create path NSString *[email protected] "Http://192.168.1.53:8080/MJServer/video"; Nsurl *url=[nsurl URLWITHSTRING:URLSTR]; 31 32//Create request Nsmutableurlrequest *request=[nsmutableurlrequest requestwithurl:url];//default to GET request 34//Set Maximum network wait time is request.timeoutinterval=5.0; 36 37//Get home row Nsoperationqueue *queue=[nsoperationqueue Mainqueue]; 39//Initiation Request [Nsurlconnection SendasynchronouSrequest:request queue:queue completionhandler:^ (nsurlresponse *response, NSData *data, NSError *connectionError) {41 Hide HUD [Mbprogresshud Hidehud]; if (data) {//If the request succeeds, get the data returned by the server 44//parse the data received (JSON mode) Nsdictionary *dict=[nsjsonserializa tion Jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:nil]; *//Nsarray *array=dict[@ "video"]; Nsarray *array=dict[@ "Videos"]; Nsmutablearray *videos=[nsmutablearray Array]; (nsdictionary *dict in array) {Wuyi Yyviodesmodel *model=[yyviodesmodel viodesmodelwithdic T:DICT]; [Videos Addobject:model]; Self.videos=videos}; 55 56//Refresh form [Self.tableview Reloaddata]; }else//If the request failed, did not get the data {"Mbprogresshud showerror:@" network busy, and then try again later! "]; 62} 63 64}]; 65} 66 67#pragma mark-Data source Method-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {1 return; Nteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section of the Self.videos.count; 7 5}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath Tatic nsstring *[email protected] "ID"; UITableViewCell *cell=[tableview Dequeuereusablecellwithidentifier:id]; if (cell==nil) {Bayi Cell=[[uitableviewcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseIdentifier : ID]; 82} 83 84//Get Data Model Yyviodesmodel *model=self.videos[indexpath.row]; Cell.textlabel.text=model.name; NSString *length=[nsstring stringwithformat:@ "duration%d minutes", model.length]; Cell.detailtextlabel.text=length; Video.image = = Resources/images/minion_01.png NSString *imageurl = [NSString stringwithformat:@ "H ttp://192.168.1.53:8080/mjserver/%@ ", Model.image]; 92 93//The third-party framework is used here 94 [Cell.imageview Setimagewithurl:[nsurl Urlwithstring:imageurl] placeholderimage:[uiimage i magenamed:@ "placeholder"]; The return cell; 97} 98 99//Set cell row height-(cgfloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexPath101 {102 return 70;103}104 @end

Description: Third-party frameworks Mbprogresshud and sdwebimage are used in this project.

Simulator Case:

Ii. adjusting the internal structure of the cell

Tip: It is not feasible to set the height of the cell's neutron control slices in the following method. The following method value is responsible for processing the data of the cell, after returning to the cell, the system's method is called to set the cell's internal structure, if the frame is set in the method, then it will be overwritten, is invalid.

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{....}

Implementation ideas:

(1) You can create a new xib and use the Xib settings.

(2) Create a new class that inherits from Uitablecell and is set in the-(void) Layoutsubviews of the class (note: Be sure to call the Layoutsubviews method of the parent class first).

The second approach is shown below:

Set frame in-(void) layoutsubviews

 1//2//YYCELL.M 3//01-Text top-top client 4//5//Created by Apple on 14-6-29. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYCell.h" @interface Yycell () @property (nonatomic,weak) UIView * divider;13 @end14 @implementat  Ion YYCell15-(ID) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) ReuseIdentifier17 {self = [Super Initwithstyle:style reuseidentifier:reuseidentifier];19 if (self) {20 21//Add a line @ UIView *di         Vider=[[uiview alloc]init];23 [Divider setbackgroundcolor:[uicolor browncolor]];24 divider.alpha=0.5;25 [Self.contentview addsubview:divider];26 self.divider=divider;27}29 return self;30}31 3 2-(void) LayoutSubviews33 {[Super layoutsubviews];35 36//Adjust Frame37//Picture frame38 cgfloat imagex=10;     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//title of Frame45 CGRect textf=self.textlabel.frame;46 textf.origin.x=imagew+2*imagex;47 self.textlabel.frame=textf;48 49//Small title of Frame50 CGRect detailtextf=self.     detailtextlabel.frame;51 detailtextf.origin.x=textf.origin.x;52 self.detailtextlabel.frame=detailtextf;53 54 Set underline frame55 cgfloat dividerh=1.0;56 cgfloat dividerw=self.frame.size.width;57 cgfloat Dividery=self.fra me.size.height-1;58 self.divider.frame=cgrectmake (0, Dividery, Dividerw, dividerh);}60 @end

Main controller file code:

  1//2//YYVIEWCONTROLLER.M 3//01-Text top-top client 4//5//Created by Apple on 14-6-29. 6//Copyright (c) 2014 itcase.  All rights reserved. 7//8 9 #import "YYViewController.h" #import "mbprogresshud+mj.h" #import "YYviodesModel.h" #import "Uiimagev  Iew+webcache.h "#import" YYCell.h "@interface Yyviewcontroller () @property (Nonatomic,strong) Nsarray *videos; @end @implementation Yyviewcontroller-(void) viewdidload [viewdidload]; 25// Remove underline self.tableview.separatorstyle=uitableviewcellseparatorstylenone; [Mbprogresshud showmessage:@ "is trying to load"]; 29 30//Create path NSString *[email protected] "Http://192.168.1.53:8080/MJServer/video"; Nsurl *url=[nsurl URLWITHSTRING:URLSTR]; 34 35//Create request Nsmutableurlrequest *request=[nsmutableurlrequest requestwithurl:url];//default to GET request 37//Set Maximum network wait time of request.timeoutinterval=20.0; 39 40//Get home Row 41    Nsoperationqueue *queue=[nsoperationqueue Mainqueue]; 42//Initiation Request [Nsurlconnection sendasynchronousrequest:request queue:queue completionhandler:^ (NSURLResponse *respon SE, NSData *data, Nserror *connectionerror) {44//Hide HUD [Mbprogresshud Hidehud]; /If the request succeeds, get the data returned by the server 47//parse the data received (JSON mode) nsdictionary *dict=[nsjsonserialization Jsonobjectwithdat A:data options:nsjsonreadingmutableleaves Error:nil]; //Nsarray *array=dict[@ "video"]; Nsarray *array=dict[@ "Videos"]; Wuyi Nsmutablearray *videos=[nsmutablearray Array]; (nsdictionary *dict in array) {Yyviodesmodel *model=[yyviodesmodel viodesmodelwithdic T:DICT]; [Videos Addobject:model]; Self.videos=videos; 58 59//Refresh Table [Self.tableview Reloaddata]; }else//If the request fails, you don't get the data 63       {[Mbprogresshud showerror:@] Network busy, and then try again later! "]; 65} 66 67}]; #pragma mark-Data source method (Nsinteger) Numberofsectionsintableview: (UITableView *) TableView (1); 7 4} (Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section Self.vid Eos.count;     (UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath 80 {81 Static NSString *[email protected] "ID"; Yycell *cell=[tableview Dequeuereusablecellwithidentifier:id];     if (cell==nil) {Cell=[[yycell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:id]; 85 } 86 87//Get Data Model Yyviodesmodel *model=self.videos[indexpath.row]; Cell.textlabel.text=model.name; NSString *length=[nsstring stringwithformat:@ "duration%d minutes", model.length]; Cell.detailtextlabel.text=length; Video.image = Resources/imaGes/minion_01.png 94 NSString *imageurl = [NSString stringwithformat:@ "http://192.168.1.53:8080/mjserver/%@", model.i Mage]; 95 96//The third-party framework is used here [Cell.imageview Setimagewithurl:[nsurl Urlwithstring:imageurl] placeholderimage:[uiimage i magenamed:@ "placeholder"]; 98 return cell;100}101 102//Set cell row Height 103-(cgfloat) TableView: (UITableView *) TableView Heightforrowatindexpat H: (Nsindexpath *) indexPath104 {70;106}107 @end

Simulator Run Effect:

iOS Development Web chapter-Implementing a video playback client small application (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.