iOS development UI-use dynamic cells in UITableView applications to build an app management interface
First, the realization effect
Description: This example is done using dynamic cells in storyboard.
Second, the realization
1. project file structure and plist file
2. Implementation process and code
Select the dynamic cell in the TableView property selector.
Description: Complete the definition of Tableviewcell using its own dynamic cell directly in storyboard, and create a class that manages the cell, which is wired.
Implementation code:
Data Model section:
YYappInfo.h file
1//2// YYappInfo.h 3// 01-Use dynamic cells to complete the application management interface of the app 4//5// Created by Hole Doctor on 14-6-2.6// Copyright (c) Itcast 2014. All rights reserved. 7//8 9 #import <foundation/foundation.h>10 @interface yyappinfo:nsobject12 @property (nonatomic,copy) NSString *size;13 @property (nonatomic,copy) nsstring *download;14 @property (nonatomic,copy) nsstring *icon;15 @ Property (nonatomic,copy) NSString *name;16-(Instancetype) Initwithdict: (Nsdictionary *) dict;20 + (instancetype ) Appinfowithdict: (nsdictionary *) dict;21 @end
YYAPPINFO.M file
1//2// YYAPPINFO.M 3// 01-Use dynamic cells to complete the application management interface of the app 4//5// Created by Hole Doctor on 14-6-2.6// Copyright (c) Itcast 2014. All rights reserved. 7//8 9 #import "YYappInfo.h" @implementation YYappInfo12-(Instancetype) Initwithdict: (Nsdictionary *) dict14 { Self=[super init] { //use KVC17 [self setvaluesforkeyswithdictionary:dict];18 }19 return self;20}21 + (instancetype) appinfowithdict: (Nsdictionary *) Dict24 {[Self alloc] initwithdict:dict];27}28 @end
View section
YYappCell.h file
1//2// YYappCell.h 3// 01-Use dynamic cells to complete the application management interface of the app 4//5// Created by Hole Doctor on 14-6-2.6// Copyright (c) Itcast 2014. All rights reserved. 7//8 9 #import <uikit/uikit.h>10 @class yyappinfo,yyappcell;13 @protocol yyappcelldelegate <nsobje Ct>15-(void) Btndidclick: (Yyappcell *) cell;16 @end19 @interface yyappcell:uitableviewcell20 @property (nonat Omic,strong) Yyappinfo *app;22//@property (Nonatomic,strong) Yyviewcontroller *controller;23 @property (nonatomic, Strong) ID <YYappCellDelegate> delegate;24 @end
YYAPPCELL.M file
1//2//YYAPPCELL.M 3//01-Use dynamic cells to complete the application management interface of the app 4//5//Created by hole Medical self on 14-6-2. 6//Copyright (c) 2014 itcast. All rights reserved. 7//8 9 #import "YYappCell.h" #import "YYappInfo.h" @interface Yyappcell () @property (weak, nonatomic) Iboutle T Uiimageview *appimg;14 @property (weak, nonatomic) Iboutlet UILabel *apptitle;16 @property (weak, nonatomic) Iboutlet UILabel *appdownload;17 @property (weak, nonatomic) Iboutlet UIButton *appbtn;18 @end20 @implementation YYappCell21 22 -(void) Setapp: (Yyappinfo *) app24 {_app=app;26 self.apptitle.text=_app.name;27 self.appdownload.text=[ns String stringwithformat:@ "Size%@ | Download volume%@ times ", _app.size,_app.download];28 self.appimg.image=[uiimage imagenamed:_app.icon];29}31 #pragma mark-finish button click Event-(ibaction) Btnonclick: (UIButton *) SENDER35 {36//button is clicked, becomes unavailable after the sender.enabled=no;38 39// Notifies the agent to complete the prompt to download the completed animation effect in the [Self.delegate respondstoselector: @selector (btndidcliCK:)] {41//In general, whoever triggers will pass it on. [Self.delegate btndidclick:self];43}44}45 @end
Master Controller
YYVIEWCONTROLLER.M file
1//2//YYVIEWCONTROLLER.M 3//01-Use dynamic cells to complete the application management interface of the app 4//5//Created by hole Medical self on 14-6-2. 6//Copyright (c) 2014 itcast. All rights reserved. 7//8 9 #import "YYViewController.h" #import "YYappInfo.h" #import "YYappCell.h" @interface Yyviewcontrol Ler () <UITableViewDataSource,YYappCellDelegate> @property (nonatomic,strong) Nsarray *apps; @property (Strong, nonatomic) Iboutlet UITableView *tableview; @end @implementation Yyviewcontroller-(void) viewdidload [[Super Viewdidload]; 24} 25 2 6 #pragma mark-use lazy loading first to load the data in the Plist file in the (Nsarray *) Apps (_apps==nil) {NSString *fullpath=[[n Sbundle mainbundle]pathforresource:@ "Apps_full.plist" oftype:nil]; Nsarray *arraym=[nsarray Arraywithcontentsoffile:fullpath]; Nsmutablearray *modles=[nsmutablearray ArrayWithCapacity:arrayM.count]; Nsdictionary *dict in Arraym) {Yyappinfo *appinfo=[yyappinfo Appinfowithdict:dict]; [Modles Addobject:appinfo]; PNS _apps=[modles Copy]; (_apps}) 45 mark-Set TableView Data source method//group-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView (Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) Sect (49} 50/Line) Ion Self.apps.count {54} 55//group-row-data--(UITableViewCell *) TableView: (UITableView *) TableView cellfor Rowatindexpath: (Nsindexpath *) Indexpath 57 {58//Create cell-static NSString *[email protected] "app"; Yappcell *cell=[tableview Dequeuereusablecellwithidentifier:identifier]; 61//Set data for cell Yyappinfo *appinfo=self.apps[indexpath.row]; 63//Set Agent cell.delegate=self; Cell.app=appinfo; 66//Return cell; #pragma mark-set agent-(void) Btndidclick: (Yyappcell *) cell 72 {73//Take Out model YyappinFo *app=cell.app; NSLog (@ "Daili"); UILabel *lab=[[uilabel Alloc]init]; 77//Display location of the hint Lab.frame=cgrectmake (60, 300, 200, 20); 79//Set hint text lab.text=[nsstring stringwithformat:@ "%@ has been downloaded", App.name]; 81//Set text background color [lab Setbackgroundcolor:[uicolor Graycolor]]; [Self.view Addsubview:lab]; lab.alpha=1.0; 85 86//Set animation effect [UIView animatewithduration:2.0 animations:^{lab.alpha=0.0; : ^ (BOOL finished) {90//Remove the popup message from the parent view [Lab Removefromsuperview]; 92}]; 94 #pragma mark-hidden status bar (BOOL) Prefersstatusbarhidden (98 return YES;}100 101 @end
Additional Information
The corresponding cell is taken out of the program with the identifier, because the cell has been tagged with an identifier (APP) in the storyboard.
Group-row-Data-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{ //Create cell static nsstring *[email protected] "app"; Yyappcell *cell=[tableview Dequeuereusablecellwithidentifier:identifier]; Set the cell data yyappinfo *appinfo=self.apps[indexpath.row]; Set proxy cell.delegate=self; Cell.app=appinfo; Returns cell return cell;
iOS development UI-use dynamic cells in UITableView applications to build an app management interface