Basic tutorial on dynamic and static cells in UI production developed by IOS _ios

Source: Internet
Author: User
Tags reserved uikit

Use of static cells
One, the realization effect and the explanation

Note: Observe the above display effect, you can find that the entire interface is displayed by a tableview, the above data are fixed, and almost will not change.

There are several ways to accomplish the effect above:

(1) can use the code directly, return three groups, in the judgement of how many rows per group, show what data, so write "dead" code suggested never to use.

(2) A little more flexible, you can put the plist file a lazy load of the way, loaded into the program, dynamic acquisition. But looking at the interface structure, it is easy to see that this requires a model nesting, very cumbersome.

(3) Storyboard provides the function of static cell, it is convenient to complete the above interface display effect. (Hint: rarely used in actual development)

Second, the use of static cells to complete the simple interface display process

In a similar development, if the entire interface is TableView, then direct the controller to inherit from Uitableviewcontroller.

Modify the master controller to inherit from the Uitableviewcontroller

Delete the default UIView in the storyboard and drag a Viewcontroller

When dragged into a viewcontroller, it defaults to a cell, by default, the cell is dynamic and is invisible by default.

To set the cell to static, as seen in the content of the property panel as static cell (static cell) The result note must change this property here.

To associate it with the host controller

Next, you can set the displayed picture and text in turn.

There are two ways to set a caption:

1 is double click Change

2 is to click the child control lable Modify

Set up secondary views as required by the interface

Set how many groups there are, and how many rows per group.
Set up Group:
Click TableView to set the sections property of the Properties panel.

Set how many rows per group:

Tip: If you write a cell for thousands of years, then you can write a line in a group, and then copy, slightly modified.
Note: Static cell is actually developed, rarely used, here only knowledge points introduced.


use dynamic cell in UITableView application to complete the application management interface of app
First, the realization effect

Description: This example uses dynamic cells in storyboard to complete.

Second, realize

1. Project document structure and plist document

2. Implementation process and code

Select the dynamic cell in the TableView property selector.

Note: In storyboard, use its own dynamic cell to complete the definition of Tableviewcell, and create a class to manage the cell, and make a connection.

Implementation code:

Data Model section:

YYappInfo.h file

Copy Code code as follows:

//
YYappInfo.h
01-Use dynamic cells to build the app application management interface
//
Created by Hole medical self on 14-6-2.
Copyright (c) 2014 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Yyappinfo:nsobject
@property (nonatomic,copy) NSString *size;
@property (nonatomic,copy) NSString *download;
@property (nonatomic,copy) NSString *icon;
@property (nonatomic,copy) NSString *name;

-(Instancetype) Initwithdict: (Nsdictionary *) dict;
+ (Instancetype) appinfowithdict: (Nsdictionary *) dict;
@end


YYAPPINFO.M file
Copy Code code as follows:

//
Yyappinfo.m
01-Use dynamic cells to build the app application management interface
//
Created by Hole medical self on 14-6-2.
Copyright (c) 2014 itcast. All rights reserved.
//

#import "YYappInfo.h"

@implementation Yyappinfo

-(Instancetype) Initwithdict: (Nsdictionary *) dict
{
if (Self=[super init]) {
Using KVC
[Self setvaluesforkeyswithdictionary:dict];
}
return self;
}


+ (Instancetype) appinfowithdict: (Nsdictionary *) dict
{

return [[Self alloc]initwithdict:dict];
}
@end


View section

YYappCell.h file

Copy Code code as follows:

//
YYappCell.h
01-Use dynamic cells to build the app application management interface
//
Created by Hole medical self on 14-6-2.
Copyright (c) 2014 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>


@class Yyappinfo,yyappcell;

@protocol yyappcelldelegate <NSObject>
-(void) Btndidclick: (Yyappcell *) cell;


@end
@interface Yyappcell:uitableviewcell

@property (Nonatomic,strong) Yyappinfo *app;
//@property (Nonatomic,strong) Yyviewcontroller *controller;
@property (nonatomic,strong) ID <YYappCellDelegate> delegate;

@end


YYAPPCELL.M file
Copy Code code as follows:

//
yyappcell.m
01-Use dynamic cells to build the app application management interface
//
Created by Hole medical self on 14-6-2.
Copyright (c) 2014 itcast. All rights reserved.
//

#import "YYappCell.h"
#import "YYappInfo.h"

@interface Yyappcell ()
@property (Weak, nonatomic) Iboutlet Uiimageview *appimg;

@property (Weak, nonatomic) Iboutlet Uilabel *apptitle;
@property (Weak, nonatomic) Iboutlet Uilabel *appdownload;
@property (Weak, nonatomic) Iboutlet UIButton *appbtn;

@end


Copy Code code as follows:

@implementation Yyappcell


-(void) Setapp: (Yyappinfo *) app
{
_app=app;
Self.apptitle.text=_app.name;
self.appdownload.text=[nsstring stringwithformat:@ "size%@ | Download amount%@ times", _app.size,_app.download];
Self.appimg.image=[uiimage Imagenamed:_app.icon];

}

#pragma mark-Complete button click event

-(Ibaction) Btnonclick: (UIButton *) sender
{
When the button is clicked, it becomes unavailable
Sender.enabled=no;

Notify the agent to complete the prompts to download the completed animation effect
if ([Self.delegate respondstoselector: @selector (Btndidclick:)]) {
Generally speaking, whoever triggers the transmission will pass
[Self.delegate btndidclick:self];
}
}

@end


Master Controller

YYVIEWCONTROLLER.M file

Copy Code code as follows:

//
Yyviewcontroller.m
01-Use dynamic cells to build the app application management interface
//
Created by Hole medical self on 14-6-2.
Copyright (c) 2014 itcast. All rights reserved.
//

#import "YYViewController.h"
#import "YYappInfo.h"
#import "YYappCell.h"

@interface Yyviewcontroller () <UITableViewDataSource,YYappCellDelegate>
@property (Nonatomic,strong) Nsarray *apps;
@property (Strong, nonatomic) Iboutlet UITableView *tableview;

@end


Copy Code code as follows:

@implementation Yyviewcontroller

-(void) viewdidload
{
[Super Viewdidload];
}

#pragma mark-use lazy loading to first load the data in the Plist file.
-(Nsarray *) apps
{
if (_apps==nil) {
NSString *fullpath=[[nsbundle mainbundle]pathforresource:@ "Apps_full.plist" oftype:nil];
Nsarray *arraym=[nsarray Arraywithcontentsoffile:fullpath];

Nsmutablearray *modles=[nsmutablearray ArrayWithCapacity:arrayM.count];
For (Nsdictionary *dict in Arraym) {
Yyappinfo *appinfo=[yyappinfo Appinfowithdict:dict];
[Modles Addobject:appinfo];
}
_apps=[modles copy];
}
return _apps;
}


#pragma mark-Set the TableView data source method
Group
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
{
return 1;
}
Yes
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
{
return self.apps.count;
}
Group-Rows-data
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath
{
Creating a Cell
Static NSString *identifier=@ "app";
Yyappcell *cell=[tableview Dequeuereusablecellwithidentifier:identifier];
Set data for a cell
Yyappinfo *appinfo=self.apps[indexpath.row];
Set up agents
cell.delegate=self;
Cell.app=appinfo;
Back to Cell
return cell;
}

#pragma mark-settings Agent
-(void) Btndidclick: (Yyappcell *) cell
{
Take out the model
Yyappinfo *app=cell.app;
NSLog (@ "Daili");
Uilabel *lab=[[uilabel Alloc]init];
Where the hint is displayed
Lab.frame=cgrectmake (60, 300, 200, 20);
Set hint text
Lab.text=[nsstring stringwithformat:@ "%@ has been downloaded to complete", app.name];
Set Text background color
[Lab Setbackgroundcolor:[uicolor Graycolor]];
[Self.view Addsubview:lab];
lab.alpha=1.0;

Set animation effects
[UIView animatewithduration:2.0 animations:^{
lab.alpha=0.0;
} completion:^ (BOOL finished) {
Remove the pop-up prompts from the parent view
[Lab Removefromsuperview];
}];
}

#pragma mark-Hide Status bar
-(BOOL) Prefersstatusbarhidden
{
return YES;
}

@end


Supplementary notes

The corresponding cell is taken out of the program by means of an identifier because the cell has been labeled with a storyboard (app) in the box.

Copy Code code as follows:

Group-Rows-data
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath
{
Creating a Cell
Static NSString *identifier=@ "app";
Yyappcell *cell=[tableview Dequeuereusablecellwithidentifier:identifier];
Set data for a cell
Yyappinfo *appinfo=self.apps[indexpath.row];
Set up agents
cell.delegate=self;
Cell.app=appinfo;
Back to Cell
return cell;
}

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.