One, adding child controls and passing model data
Note: The child control position is not fixed and cannot be written to customize the cell with code
Step One: Change the controller to inherit Uitableviewcontroller, then delete the original view on the storyboard, drag a new TableView, and change the class to the controller
Step Two: Create a new class Weibocell, inherit from UITableViewCell
Step three: Import WeiboCell.h in the controller implementation file
Third method of data source
static NSString *id = @ "Weibo";
Weibocell *cell = [TableView dequeuereusablecellwithidentifier:id];
if (cell = = nil) {
cell = [Weibocell alloc] Initwithstyle:uitablecellstyledefault reuseidentifier:id];//Create a Weibocell, so it should be taken out of this.
}
return cell;
}
Step four: In the WEIBOCELL.M
-(ID) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (nsstring*) Reuseidentifier
{
self = [super Initwithstyle reuseidentifier:reuseidentifier];
if (self) {
Add internal sub-controls (regardless of position and height, add all that is possible)
1. Avatar
_icon = [Uiimageview alloc] init];
[Self.contentview Addsubview:_icon]; Add child controls to add to Contentview
2. Nickname
_name = [UILabel alloc] init];
[Self.contentview Addsubview:_name];
3. Member icon
_VIP = [Uiimageview alloc] initwithimage:[uiimage imagenamed:@ "Vip.png"];
[Self.contentview ADDSUBVIEW:_VIP];
4. Time
_time = [UILabel alloc] init];
[Self.contentview Addsubview:_time];
5. Source
_source = [UILabel alloc] init];
[Self.contentview Addsubview:_source];
6. Weibo text
_content = [UILabel alloc] init];
_content.numberoflines = 0; Wrap Line
[Self.contentview addsubview:_content];
7. Map
_image = [Uiimageview alloc] init];
[Self.contentview Addsubview:_image];
}
Step five: Develop the model and pass the model to the cell
plist file is ready (root is Array)
Creates a new model class Weibo, and declares 7 properties.
and write 2 ways to translate the dictionary into a model.
-(ID) initwithdict: (Nsdictionary *) dict
{
if (self = [super init]) {
Self.name = dict[@ "name"];
Self.time = dict[@ "Time"];
Self.name = dict[@ "name"];
Self.name = dict[@ "name"];
Self.name = dict[@ "name"];
Self.name = dict[@ "name"];
Self.name = dict[@ "name"];
}
}
+ (ID) weibowithdict: (Nsdictionary *) dict
{
return [[Self alloc] initwithdict:dict];
}
Step Six: Pass a microblog model in Weibocell's header file
@class Weibo;
@property (Nonatomic,strong) Weibo Weibo;
Step Seven: Override the Set method in the Weibocell implementation file to assign the model data to the child control in this method
Import Weibo.h
Declare 7 child controls as member variables in the class extension, and then change all of the controls to underline
-(void) Setweibo: (Weibo *) Weibo
{
_weibo = Weibo;
1. Avatar
_icon.image = [UIImage ImageNamed:weibo.icon];
2. Nickname
_name.text = Weibo.name;
if (_WEIBO.VIP) {
_name.textcolor = [Uicolor Yellowcolor];
} else {
_name.textcolor = [Uicolor blackcolor];
}
3. Member icon
_vip.hidden =! WEIBO.VIP;
4. Time
_time.text = Weibo.time;
5. Source
_source.text = [NSString stringwithformat:@ "from%@", _weibo.source];
6. Text
_content.text = weibo.content;
7. Map
if (weibo.image) {//have a map
_image.hidden = NO;
_image.image = [UIImage imageNamed:weibo.image];
} else {//does not have a map
_image.hidden = YES;
}
}
Step Eight: The controller loads the plist file (only needs to be loaded once, so in Viewdidload)
Nsarray *array = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle
pathforresource:@ "Weibo.plist" oftype:nil];
Import Weibo.h and declare an array member variable in the class extension to load the data
_weibos = [Nsmutablearray array];
For (nsdictionary *dict in array) {
[_weibos addobject: (Weibo weibowithdict:dict)] ;
}
}
Step nine: Implementing a data source approach
The second method of data sources
return _weibos.count;
Third method of data source
1. Remove the cell from the cache pool
static NSString *id = @ "Weibo";
Weibocell *cell = [TableView dequeuereusablecellwithidentifier:id];
2. The cache pool does not have a cell and re-creates the cell
if (cell = = nil) {
cell = [[Weibocell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:id];
}
3. Passing model data
Cell.weibo = _weibos[indexpath.row];
return cell;
}
Second, calculate the child control of the frame
Place the code snippet for the set data in the new method Settingdata, and then call this method in the Set method to
-(void) Setweibo: (Weibo *) Weibo
{
_weibo = Weibo;
1. Set up Weibo data
[Self settingdata]
2. Set the child control's frame (x,y,width,height)
[Self settingsubviewframe];
}
-(void) settingsubviewframe
{
1. Avatar
CGFloat IconX = Kcellborder;
CGFloat icony = Kcellborder;
_icon.frame = CGRectMake (ICONX,ICONY,KICONMH,KICONMH);
2. Nickname
CGFloat NameX = Cgrectgetmaxx (_icon.frame) + Kcellborder;
CGFloat Namey = icony;
Calculate the size of a user name
Cgsize namesize = [_weibo.name Sizewithfont:_name.font];
_icon.frame = CGRectMake (namex,namey,namesize.width,namesize.height)
3.vip
CGFloat vipx = Cgrectgetmaxx (_name.frame) + Kcellborder;
CGFloat vipy = Namey;
_icon.frame = CGRectMake (VIPX,VIPY,KVIPMH,KVIPMH);
4. Time
CGFloat TimeX = NameX;
CGFloat timey = Cgrectgetmaxy (_name.frame) + Kcellborder;
Cgsize timesize = [_weibo.time Sizewithfont:_time.font];
_time.frame = CGRectMake (timex,timey,timesize.width,timesize.height);
//5. SOURCE
CGFloat Sourcex = Cgrectgetmaxx (_time.frame) + kcellborder;
CGFloat sourcey = timey;
NSString *SOURC EText = [NSString stringwithformat:@ "from%@", _weibo.source];
cgsize sourcesize = [SourceText sizewithfont:_ Source.font];
_source.frame = CGRectMake (sourcex,sourcey,sourcesize.width,sourcesize.height);
6. Text
CGFloat contentx = IconX;
CGFloat contenty = MAX (Cgrectgetmaxy (_time.frame), Cgrectgetmaxy (_icon.frame)) + Kcellborder;
CGFloat contentw = Self.frame.size.width-2*kcellborder;
Calculate Text Size (displays the width of the text)
Cgsize contentsize = [_weibo.content sizewithfont:_content.font constrainedtosize:cgsizemake (ContentW,MAXFLOAT)];
_content.frame = CGRectMake (contentx,contenty,contentw,contentsize.height);
7. map \ Calculate the height of a cell
CGFloat cellheight = 0;
if (_weibo.image) {//have a map
CGFloat ImageX = ContentX;
CGFloat imagey = Cgrectgetmaxy (_content.frame) + Kcellborder;
_image.frame = CGRectMake (IMAGEX,IMAGEY,KIMAGEWH,KIMAGEWH);
CGFloat cellheight = Cgrectgetmaxy (_image.frame) + Kcellborder;
} else {
CGFloat cellheight = Cgrectgetmaxy (_content.frame) + Kcellborder;
}
Because the proxy method is called before the set method is called, the height of the egg is computed in the set method.
Create a new 1 model weiboframe a frame to hold all child controls inside a cell
Declaring properties such as iconf, there is also an attribute cellheight and an attribute Weibo (@class Weibo)
Put a large string of code for the calculation frame into the Setweibo method in the implementation file of this model
As long as you give this Weibo attribute to it, it will help you calculate the frame
Start Calculating Cell Height
Get the cell's height based on the model data
Weiboframe *f = [[Weiboframe alloc] init];
F.weibo = _weibos[indexpath.row];
return f.cellheight;
*weibocell does not need to have the Weibo attribute, but has the weiboframe attribute, so that both the Weibo data can be obtained, but also to get Weiboframe data
* Code to modify the Settingsubviewframe method
1. Avatar
_icon.frame = _weiboframe.iconf;
......
Summary of steps
1> create a new subclass of UITableViewCell----Weibocell
2> when creating the cell (Initwithstyle:reuseindetifier: method) Add the child controls that need to be used inside the cell (settings need to be set only once in the Init method)
(Note: Just create the Add child control, do not go to the location and size of the pipe control)
3> Create a new model class----Weibo to add the corresponding data properties
4> adds a Weibo model property to Weibocell, setting the properties of the child control while getting the Weibo model data
5> Rewrite Setweibo: Method: Remove the data from the Weibo model into the child controls
Add:
Registers a xib file in Viewdidload, and if TableView does not take the recyclable cell through the identity in the cache pool, the Mycell1.xib file is loaded to create the cell
[Self.tableview registernib[uinib nibwithnibname:@ "MyCell1" Bundle:nil]
forcellreuseidentifier:@ "Cell"];
As a result, it is not necessary to determine whether the cell is nil in the 3rd data source method.
iOS development-Customizing cells with code