iOS eighth day (6:uitableviewcontroller Sina Weibo, model and control location encapsulation together with Statusframe)

Source: Internet
Author: User

Hmviewcontroller

#import "HMViewController.h"#import "HMStatus.h"#import "HMStatusCell.h"#import "HMStatusFrame.h"@interfaceHmviewcontroller ()/** Save an array of statusframe models*/@property (nonatomic, strong) Nsarray*Statusframes;@end@implementationHmviewcontroller-(Nsarray *) statusframes{if(_statusframes = = nil) _statusframes =[hmstatusframe statusframes]; return_statusframes;}- (void) viewdidload{[Super Viewdidload]; //self.tableView.rowHeight = $;}#pragmaMark-Data Source Method-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{returnSelf.statusFrames.count;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *id =@"Cell"; Hmstatuscell*cell =[TableView Dequeuereusablecellwithidentifier:id]; if(Cell = =Nil) {Cell=[[Hmstatuscell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:id]; }        //Assign Value//Remove the Statusframe modelHmstatusframe *statusframe =Self.statusframes[indexpath.row]; Cell.status=Statusframe.status; in//4 returncell;}#pragmaMark-Proxy method/** Calculate cell Row Height*/-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{/** The method of calculating the row height, how many rows are calculated when the tabular data is loaded contentsize problem: When this method executes, the cell is not instantiated!          However: The row height calculation is when the cell is instantiated, by setting the Status property, the calculated = = with the status model, you can know the row height!     Question: How do I get the row height before the cell is instantiated? Workaround: The row height can be calculated by status! = "Build a model that calculates the position of all controls*/Hmstatusframe*statusframe =Self.statusframes[indexpath.row]; returnstatusframe.cellheight;}@end


Hmstatusframe

#import<Foundation/Foundation.h>@classHmstatus;/** Specifically calculates all control locations*/@interfaceHmstatusframe:nsobject@property (nonatomic, assign) CGRect iconf; @property (nonatomic, assign) CGRect NAMEF; @prope Rty (nonatomic, assign) CGRect vipf; @property (nonatomic, assign) CGRect TEXTF; @property (nonatomic, assign) CGRect Pictur EF;/** Row Height*/@property (nonatomic, assign) cgfloat cellheight;/** The size of all controls can be calculated by status*/@property (nonatomic, strong) Hmstatus*status;/** All statusframe data Arrays*/+ (Nsarray *) Statusframes;@end

****************hmstatusframe. m

#import "HMStatusFrame.h"#import "HMStatus.h"/** Name Font*/#defineKnamefont [Uifont systemfontofsize:14]/** Text Font*/#defineKtextfont [Uifont systemfontofsize:16]@interfaceHmstatusframe ()@end@implementationHmstatusframe-(void) SetStatus: (Hmstatus * ) Status {_status=status; //0. Define SpacingCGFloat padding =Ten; //1. AvatarCGFloat IconX =padding; CGFloat Icony=padding; CGFloat iconw= -; CGFloat Iconh= -; Self.iconf=CGRectMake (IconX, Icony, Iconw, Iconh); //2. Name size is determined by the length of the text//Boundingrectwithsize calculates the area occupied by a given text string//The return value is an x, y = 0 Cgrect,w,h is the calculated width and height//    //If you want to calculate the exact height of multiple rows, you need to pass in the Nsstringdrawinguseslinefragmentorigin option//Dict Dictionary that specifies the related properties of the font, the first header file in the Uikit frame//Context:nilNsdictionary *namedict =@{nsfontattributename:knamefont}; CGRect Nameframe=[Self.status.name boundingrectwithsize:cgsizemake (Maxfloat, maxfloat) options:    Nsstringdrawinguseslinefragmentorigin attributes:namedict Context:nil]; Nameframe.origin.x= Cgrectgetmaxx (self.iconf) +padding; NAMEFRAME.ORIGIN.Y= padding + (self.iconf.size.height-nameframe.size.height) *0.5; SELF.NAMEF=Nameframe; //VIP iconCGFloat vipx = Cgrectgetmaxx (SELF.NAMEF) +padding; CGFloat vipy=SELF.NAMEF.ORIGIN.Y; CGFloat VIPW= -; CGFloat Viph= -; Self.vipf=CGRectMake (vipx, vipy, VIPW, VIPH); //BodyNsdictionary *textdict =@{nsfontattributename:ktextfont}; CGRect TextFrame= [Self.status.text Boundingrectwithsize:cgsizemake ( -, maxfloat) options:nsstringdrawinguseslinefragmentorigin attributes:textdict Context:nil]; Textframe.origin.x=padding; TEXTFRAME.ORIGIN.Y= Cgrectgetmaxy (self.iconf) +padding; SELF.TEXTF=TextFrame; if(Self.status.picture.length >0) {        //Image MatchingCGFloat Picturex =padding; CGFloat Picturey= Cgrectgetmaxy (textFrame) +padding; CGFloat Picturew= -; CGFloat Pictureh= -; Self.picturef=CGRectMake (Picturex, Picturey, Picturew, Pictureh); Self.cellheight= Cgrectgetmaxy (self.picturef) +padding; } Else{self.cellheight= Cgrectgetmaxy (SELF.TEXTF) +padding; }}+ (Nsarray *)statusframes{nsarray*array = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle] Pathforresource:@"statuses.plist"Oftype:nil]]; Nsmutablearray*arraym =[Nsmutablearray array];  for(Nsdictionary *dictinchArray) {        //to add a Statusframe objectHmstatusframe *statusframe =[[Hmstatusframe alloc] init]; //instantiate a new status modelHmstatus *status =[Hmstatus statuswithdict:dict]; in//4  //  Call your own setter method, save the status data model, and calculate the position of all controls statusframe.status = status; //adding statusframe to an array[Arraym Addobject:statusframe]; }        returnArraym;}@end

iOS eighth day (6:uitableviewcontroller Sina Weibo, model and control location encapsulation together with Statusframe)

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.