-------1.1 data model. h---------
#import <Foundation/Foundation.h> @interface mlstatus:nsobject@property (nonatomic, copy) NSString *text;@ Property (nonatomic, copy) NSString *icon; @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *picture; @property (nonatomic, assign) BOOL vip;+ (instancetype) statuswithdict: (Nsdictionary *) dict;-(instancetype) Initwithdict: (nsdictionary *) dict; @end
-------1.2 data model. M---------
#import "MLStatus.h" @implementation mlstatus+ (Instancetype) statuswithdict: (nsdictionary *) dict{return [[Mlstatus Alloc]initwithdict:dict];} -(Instancetype) Initwithdict: (nsdictionary *) dict{if (self = [super init]) {//Use KVC (BOOL and int type KVC can also be converted) [Self setvaluesforkeyswithdictionary:dict]; } return self;} @end
-------the 2.1cell frame model. h---------
#import <Foundation/Foundation.h> #import <uikit/uikit.h>//If you use XCode6, CGRect and CGFloat are not available in the. h file. Need to use change header file @class mlstatus; @interface mlstatusframe:nsobject//Avatar Frame@property (nonatomic, assign, ReadOnly) CGRect iconf;//nickname of Frame@property (Nonatomic, assign, ReadOnly) cgrect namef;//member icon Frame@property (nonatomic, assign, readonly) CGRect vipf;//Body of Frame@property (nonatomic, assign, ReadOnly) CGRect textf;//with Frame@property (nonatomic, Assign, ReadOnly) cgrect Picturef;//cell height @property (nonatomic, assign, ReadOnly) CGFloat cellheight; @property ( Nonatomic, strong) Mlstatus *status; @end
-------the 2.2cell frame model. M---------
#import "MLStatusFrame.h" #import "MLStatus.h"//Nickname Font #define mlnamefont [uifont systemfontofsize:14]//font of body #define mltextfont [uifont systemfontofsize:15] @implementation mlstatusframe//Override set Method-(void) SetStatus: (mlstatus *) status{ _status = status; //the spacing between the child controls cgfloat padding = 10; //Head cgfloat iconx = padding; CGFloat iconY = padding; cgfloat iconw = 30; cgfloat iconh = 30; The //member is the ReadOnly attribute, which is equivalent to no setter method and cannot be used. Syntax method, can only be accessed by _ mode _ Iconf= cgrectmake (ICONX,&NBSP;ICONY,&NBSP;ICONW,&NBSP;ICONH); //Nickname CGSize nameSize = [self sizeWithText:self.status.name font:MLNameFont Maxsize:cgsizemake (maxfloat, maxfloat)]; cgfloat namex = Cgrectgetmaxx (_iconf) +padding; CGFloat nameY = iconY + ( Iconh - namesize.height) * 0.5; _namef = cgrectmake (NameX , namey, namesize.width, namesize.height); // Member Icon cgfloat vipx = cgrectgetmaxx (_NAMEF) + padding; cgfloat vipy = namey; cgfloat vipw = 14 ; cgfloat viph = 14; _vipf = cgrectmake (vipx, vipy, vipw, viph); //text cgfloat textx = iconx; cgfloat texty = cgrectgetmaxy (_iconF) + padding; cgsize textsize = [self sizewithtext:self.status.text font: Mltextfont maxsize:cgsizemake (300, maxfloat)]; _textf = cgrectmake ( Textx ,texty, textsize.width, textsize.height); Picture if (self.status.picture) { CGFloat pictureX = textX; CGFloat Picturey = cgrectgetmaxy (_TEXTF) + padding; CGFloat pictureW = 100; CGFloat Pictureh = 100; _picturef = cgrectmake ( Picturex, picturey, pictUrew, pictureh); _cellheight = cgrectgetmaxy (_PICTUREF) + padding; }else { _cellheight = cgrectgetmaxy (_textF) + padding; }}-(cgsize) Sizewithtext: (nsstring *) Text font: (UIFont *) font maxsize: (cgsize) maxsize{ nsdictionary *attrs = @{ nsfontattributename : font}; return [text Boundingrectwithsize: maxsize options:nsstringdrawinguseslinefragmentorigin attributes:attrs context:nil].size;} @end
-------3.1 Custom Cell.h---------
#import <UIKit/UIKit.h> @class mlstatusframe; @interface Mlstatuscell:uitableviewcell@property (Nonatomic, Strong) Mlstatusframe *statusframe;+ (instancetype) Cellwithtableview: (UITableView *) TableView; @end
-------3.2 Custom CELL.M---------
#import "MLStatusCell.h" #import "MLStatus.h" #import "MLStatusFrame.h"//Nickname Font #define Font #define mltextfont [uifont for mlnamefont [uifont systemfontofsize:14]//body SYSTEMFONTOFSIZE:15] @interface mlstatuscell () @property (nonatomic, weak) uiimageview * IconView, @property (nonatomic, weak) UILabel *nameView; @property (nonatomic, weak) uiimageview *vipview; @property (nonatomic, weak) UILabel *textView; @property (nonatomic, weak) UIImageView *pictureView; @end @implementation mlstatuscell-(instancetype) Initwithstyle: (Uitableviewcellstyle) Style reuseidentifier: (nsstring *) reuseidentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { //custom cell, be sure to add the handle to Contentview //only add all add-on controls (no data and frame) &NBSP;&NBSP;&NBSP;&NBSP;&NBsp; //Head uiimageview *iconview = [[UIImageView alloc]init]; [self.contentView addsubview:iconview]; self.iconview = iconview; //Nickname UILabel *nameView = [[UILabel alloc]init]; nameView.font = MLNameFont; [self.contentView addSubview:nameView]; self.nameview = nameview; //member Icon uiimageview *vipview = [[uiimageview alloc]init]; vipview.image = [uiimage imagenamed:@ " VIP "]; [self.contentview addsubview:vipview]; self.vipView = vipView; //Body UILabel *textView = [[UILabel alloc]init]; textView.font = MLTextFont; textview.numberoflines = 0; [self.contentview addsubview:textview]; self.textview = textview; //with map uiimageview *pictureView = [[UIImageView alloc]init]; [self.contentview addsubview:pictureview]; Self.pictureview = pictureview; } return self;} In this method, you set the frame and display data for the child control.-(void) setstatusframe: (mlstatusframe *) statusframe{ _statusframe = statusframe; //setting data for child controls [self settingdata]; //setting frame [self for child controls settingframe];} Set data-(void) settingdata{ //Weibo data mlstatus *status = self.statusframe.status; //Avatar self.iconView.image = [UIImage imageNamed:status.icon]; //Nickname self.nameview.text = status.name; //member if (STATUS.VIP) { self.vipView.hidden = NO; self.nameView.textColor = [UIColor redColor]; }else{ self.vipView.hidden = yes; self.nameview.textcolor = [uicolor blackcolor]; } //Body self.textview.text = status.text; //with Picture if (status.picture) { self.pictureView.hidden = NO; self.pictureView.image = [UIImage imageNamed:status.picture]; }else{ self.pictureview.hidden = yes; }}-(CGSize) Sizewithtext: (nsstring *) Text font: (uifont *) Font maxsize: (cgsize) maxSize{ NSDictionary *attrs = @{NSFontAttributeName : font}; return [text boundingrectwithsize: maxsize options: Nsstringdrawinguseslinefragmentorigin attributes:attrs context:nil].size;} Set frame-(void) settingframe{ //avatar self.iconview.frame = self.statusframe.iconf; //Nickname self.nameView.frame = self.statusFrame.nameF; //member Icon self.vipView.frame = self.statusFrame.vipF; //Body self.textview.frame = self.statusframe.textf; //with map if (self.statusFrame.status.picture) { self.pictureview.frame = self.statusframe.picturef; }}+ (Instancetype) Cellwithtableview: (uitableview *) tableview{ static nsstring *id = @ "Status"; mlstatuscell *cell = [tableview dequeuereusablecellwithidentifier:id]; if (Cell == nil) { cell = [[mlstatuscell alloc] initwithstyle: Uitableviewcellstyledefault reuseidentifier:id]; } return cell;} @end
-------4.1 Custom controller. h---------
#import <UIKit/UIKit.h> @interface Mlviewcontroller:uitableviewcontroller@end
-------4.2 Custom controller. M---------
#import "MLViewController.h" #import "MLStatus.h" #import "MLStatusCell.h" #import " MLStatusFrame.h "@interface MLViewController () @property (Nonatomic, strong) nsarray * Statusframes; @end @implementation mlviewcontroller- (void) viewdidload { [ super viewdidload]; Self.tableview.showsverticalscrollindicator = no;} -(nsarray *) statusframes{ if (_statusframes == nil) { //get Plist's full path nsstring *path = [[nsbundle mainbundle] pathforresource:@ "Statuses.plist" ofType : nil]; //Load Array NSArray *dictArray = [NSArray Arraywithcontentsoffile:path]; // Convert all the dictionaries inside the dictarray into models and put them in a new array nsmutablearray *statusframes = [nsmutablearray array]; for (NSDictionary *dict in dictarray) { // Create a mlstatus model mlstatus *status = [MLStatus statusWithDict:dict]; //Creating Mlstatusframe Models MLStatusFrame *statusFrame = [[mlstatusframe alloc]init]; Statusframe.status = status; [statusFrames addObject:statusFrame]; } _statusFrames = statusFrames; } return _statusframes;} -(Nsinteger) TableView: (uitableview *) Tableview numberofrowsinsection: (Nsinteger) section{ return self.statusframes.count;} -(uitableviewcell *) TableView: (uitableview *) Tableview cellforrowatindexpath: (NSIndexPath *) indexpath{ //Create cell mlstatuscell *cell = [ mlstatuscell cellwithtableview:tableview]; //Setting Height cell.statusframe = self.statusframes[indexpath.row]; //return cell return cell;} -(BOOL) Prefersstatusbarhidden{ return yes;} #pragma mark - Implement proxy method//Custom no cell height-(cgfloat) TableView: (uitableview *) tableview Heightforrowatindexpath: (nsindexpath *) indexpath{ // take out this line corresponding frame model MLStatusFrame *statusFrame = self.statusFrames[indexPath.row]; return statusframe.cellheight;} @end
ISO custom cell (cell height inconsistency) through code