// Register an XIB File
// If tableview uses the @ "cell" flag to go to the cell in the cache pool where no channels can be recycled, The mycell1.xib file will be loaded to create the cell (ios5.0)
[Self. tableview registernib: [uinib nibwithnibname: @ "mycell" Bundle: Nil] forcellreuseidentifier: @ "cell"];
// If the cell is created through a class file
// If tableview uses the @ "cell" flag to go To the cache pool and there is no channel for recycling the cell, it will create the cell (ios6.0) through the mycell file)
[Self. tableview registerclass: [mycell class] forcellreuseidentifier: @ "cell"];
Take Weibo as an example:
(Remember to assign values when rewriting the Set Method) -Weiboframe file: stores the frame data of each control-. the hfile declares a series of attributes to store the frame of each control. The "Data Model" attribute must be included. -- Weibo adds a property for storing the cell height-cellheight-. in the M file, rewrite the setweibo method, calculate the location of each control in the method, set the frame of each control, and finally calculate the overall height-cellheight-> weibomodel file: store Weibo Data Model->. in the H file, each attribute is declared as a class method and an object method for initialization. The parameter is dictionary-. the M file implements two initialization methods to assign the data in the dictionary to the corresponding properties-> weibocell file: inherited from uitableviewcell, stored in the cell controls->. the hfile has a weiboframe type Attribute-". the M file declares a series of member variables in the weibocell class extension, corresponding to the control to be displayed override the initialization method of the parent class initwithstyle method to initialize the control, when initializing the control, you need to initialize the member variables and then add them to self. in contentview (the frame of the control does not need to be considered during initialization), rewrite the setweiboframe method, initialize the data in the method, and then set the frame-> viewcontroller file of the Child control: in viewdidload, the Controller reads the plist file, creates data arr, and then creates a data model from the dictionary stored in arr, assign a value to the weiboframe's Weibo attribute, add the weiboframe type variables to the array, call the data source method, return the number of entries displayed in each group, and pass the data in the array to the weiboframe attribute in weibocell to call the proxy method., returns the height of each row, that is, the cellheight attribute value of the corresponding row.
Viewcontroller. m
# Import "viewcontroller. H"
# Import "weibocell. H"
# Import "Weibo. H"
# Import "weiboframe. H"
@ Interface viewcontroller ()
{
Nsmutablearray * _ weiboframes;
}
@ End
@ Implementation viewcontroller
-(Void) viewdidload {
[Super viewdidload];
Nsarray * arr = [nsarray arraywithcontentsoffile: [[nsbundle mainbundle] pathforresource: @ "Weibo. plist" oftype: Nil];
_ Weiboframes = [nsmutablearray array];
For (nsdictionary * dict in ARR ){
Weiboframe * weibof = [[weiboframe alloc] init];
Weibof. Weibo = [Weibo weibowithdict: dict];
[_ Weiboframes addobject: weibof];
}
}
-(Nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section
{
Return _ weiboframes. count;
}
-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath
{
Static nsstring * id = @ "Weibo ";
Weibocell * cell = [tableview dequeuereusablecellwithidentifier: Id];
If (cell = nil ){
Cell = [[weibocell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: Id];
}
Cell. weiboframe = _ weiboframes [indexpath. Row];
Return cell;
}
-(Cgfloat) tableview :( uitableview *) tableview heightforrowatindexpath :( nsindexpath *) indexpath
{
Return [_ weiboframes [indexpath. Row] cellheight];
}
@ End
Weibo. h
@ Interface Weibo: nsobject
@ Property (nonatomic, copy) nsstring * content; // content
@ Property (nonatomic, copy) nsstring * time; // time
@ Property (nonatomic, copy) nsstring * Source; // Source
@ Property (nonatomic, copy) nsstring * icon; // Avatar
@ Property (nonatomic, copy) nsstring * Name; // nickname
@ Property (nonatomic, copy) nsstring * image; // matching Diagram
@ Property (nonatomic, assign) bool VIP;
+ (ID) weibowithdict :( nsdictionary *) dict;
-(ID) initwithdict :( nsdictionary *) dict;
@ End
Weibo. m
@ Implementation Weibo
+ (ID) weibowithdict :( nsdictionary *) dict
{
Return [[self alloc] initwithdict: dict];
}
-(ID) initwithdict :( nsdictionary *) dict
{
If (Self = [Super init]) {
Self. Name = dict [@ "name"];
Self. Time = dict [@ "time"];
Self. VIP = [dict [@ "VIP"] boolvalue];
Self. Source = dict [@ "Source"];
Self. Image = dict [@ "image"];
Self. Icon = dict [@ "icon"];
Self. content = dict [@ "content"];
}
Return self;
}
@ End
Weibocell. h
@ Class weiboframe;
@ Interface weibocell: uitableviewcell
@ Property (nonatomic, weak) weiboframe * weiboframe;
@ End
Weibocell. m
@ Interface weibocell ()
{
Uiimageview * _ icon;
Uilabel * _ name;
Uiimageview * _ VIP;
Uilabel * _ time;
Uilabel * _ source;
Uilabel * _ content;
Uiimageview * _ image;
}
@ End
@ Implementation weibocell
-(Instancetype) initwithstyle :( uitableviewcellstyle) style reuseidentifier :( nsstring *) reuseidentifier
{
Self = [Super initwithstyle: style reuseidentifier: reuseidentifier];
If (Self ){
// 1. Avatar
_ Icon = [[uiimageview alloc] init];
[Self. contentview addsubview: _ icon];
// 2. Nickname
_ Name = [[uilabel alloc] init];
_ Name. font = knamefont;
[Self. contentview addsubview: _ name];
// 3. Member icon
_ VIP = [[uiimageview alloc] initwithimage: [uiimage imagenamed: @ "05.jpg"];
[Self. contentview addsubview: _ VIP];
// 4. Time
_ Time = [[uilabel alloc] init];
_ Time. font = ktimefont;
_ Time. textcolor = [uicolor graycolor];
[Self. contentview addsubview: _ time];
// 5. Source
_ Source = [[uilabel alloc] init];
_ Source. font = ksourcefont;
_ Source. textcolor = [uicolor graycolor];
[Self. contentview addsubview: _ source];
// 6. Weibo text
_ Content = [[uilabel alloc] init];
_ Content. font = kcontentfont;
_ Content. numberoflines = 0; // line feed
[Self. contentview addsubview: _ content];
// 7. Configure the image
_ Image = [[uiimageview alloc] init];
[Self. contentview addsubview: _ image];
}
Return self;
}
-(Void) setweiboframe :( weiboframe *) weiboframe
{
_ Weiboframe = weiboframe;
[Self settingdata];
[Self settingsubviewframe];
}
-(Void) settingsubviewframe
{
// 1. Avatar
_ Icon. Frame = _ weiboframe. iconf;
// 2. Nickname
_ Name. Frame = _ weiboframe. namef;
// 3.vip
_ VIP. Frame = _ weiboframe. vipf;
// 4. Time
_ Time. Frame = _ weiboframe. timef;
// 5. Source
_ Source. Frame = _ weiboframe. sourcef;
// 6. Body
_ Content. Frame = _ weiboframe. contentf;
// 7. Configure the image
If (_ weiboframe. Weibo. Image)
{
_ Image. Frame = _ weiboframe. imagef;
}
}
-(Void) settingdata
{
Weibo * Weibo = _ weiboframe. Weibo;
// 1. Avatar
_ Icon. Image = [uiimage imagenamed: Weibo. Icon];
// 2. Nickname
_ Name. Text = Weibo. Name;
If (Weibo. VIP ){
_ Name. textcolor = [uicolor redcolor];
} 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. Body
_ Content. Text = Weibo. content;
// 7. Configure the image
If (Weibo. Image ){
_ Image. Hidden = no;
_ Image. Image = [uiimage imagenamed: Weibo. Image];
} Else {
_ Image. Hidden = yes;
}
}
@ End
Weiboframe. h
# Define knamefont [uifont systemfontofsize: 15]
# Define ktimefont [uifont systemfontofsize: 12]
# Define ksourcefont [uifont systemfontofsize: 12]
# Define kcontentfont [uifont systemfontofsize: 15]
@ Class Weibo;
@ Interface weiboframe: nsobject
@ Property (nonatomic, assign, readonly) cgrect iconf; // Avatar
@ Property (nonatomic, assign, readonly) cgrect namef; // User Name
@ Property (nonatomic, assign, readonly) cgrect vipf; // VIP
@ Property (nonatomic, assign, readonly) cgrect timef; // time
@ Property (nonatomic, assign, readonly) cgrect sourcef; // Source
@ Property (nonatomic, assign, readonly) cgrect contentf; // body
@ Property (nonatomic, assign, readonly) cgrect imagef; // picture
// Height of a cell
@ Property (nonatomic, assign, readonly) cgfloat cellheight;
// Data Model
@ Property (nonatomic, strong) Weibo * Weibo;
@ End
Weiboframe. m
# Define kcellborder 10 // margin
# Define kiconwh 40 // Avatar width and height
# Define kvipwh 20 // width and height of the VIP icon
# Define kimagewh 80 // image width and height
@ Implementation weiboframe
-(Void) setweibo o :( Weibo *) Weibo
{
_ Weibo = Weibo;
// 1. Avatar
Cgfloat iconx = kcellborder;
Cgfloat icony = kcellborder;
_ Iconf = cgrectmake (iconx, icony, kiconwh, kiconwh );
// 2. Nickname
Cgfloat namex = cgrectgetmaxx (_ iconf) + kcellborder;
Cgfloat namey = icony;
Cgsize namesize = [_ Weibo O. Name sizewithfont: knamefont];
_ Namef = cgrectmake (namex, namey, namesize. Width, namesize. Height );
// 3.vip
Cgfloat vipx = cgrectgetmaxx (_ namef) + kcellborder;
Cgfloat vipy = namey;
_ Vipf = cgrectmake (vipx, vipy, kvipwh, kvipwh );
// 4. Time
Cgfloat Timex = namex;
Cgfloat timey = cgrectgetmaxy (_ namef) + kcellborder;
Cgsize timesize = [_ Weibo O. Time sizewithfont: ktimefont];
_ Timef = cgrectmake (Timex, timey, timesize. Width, timesize. Height );
// 5. Source
Cgfloat sourcex = cgrectgetmaxx (_ timef) + kcellborder;
Cgfloat sourcey = timey;
Nsstring * sourcetext = [nsstring stringwithformat: @ "from % @", _ Weibo. Source];
Cgsize sourcesize = [sourcetext sizewithfont: ksourcefont];
_ Sourcef = cgrectmake (sourcex, sourcey, sourcesize. Width, sourcesize. Height );
// 6. Body
Cgfloat contentx = iconx;
Cgfloat contenty = max (cgrectgetmaxy (_ timef), cgrectgetmaxy (_ iconf) + kcellborder );
Cgfloat contentw = 375-2 * kcellborder;
// Calculate the text size (display the text width)
Cgsize contentsize = [_ Weibo O. Content sizewithfont: kcontentfont constrainedtosize: cgsizemake (contentw, maxfloat)];
_ Contentf = cgrectmake (contentx, contenty, contentw, contentsize. Height );
// 7. Configure the image
If (_ Weibo. Image)
{
Cgfloat imagex = contentx;
Cgfloat imagey = cgrectgetmaxy (_ contentf) + kcellborder;
_ Imagef = cgrectmake (imagex, imagey, kimagewh, kimagewh );
_ Cellheight = cgrectgetmaxy (_ imagef) + kcellborder;
} Else {
_ Cellheight = cgrectgetmaxy (_ contentf) + kcellborder;
}
}
@ End
Advanced Development 19 _ custom cell through code