iOS no sister project-pull down on the Essence module notes, date display, refactoring sub-controller, calculate cell height (only once), Picture Post display

Source: Internet
Author: User
Tags dateformat difference between two times

I. Pull-down considerations

    • Use the pull-up controls in Mjrefresh to set the transparency automatically
    • When requesting the next page data through page, note that the pull load more data failure, the drop-down load data failed, the number of pages should be restored. Or when the request succeeds, the page changes

Two. Date display issue in post (use of two classes for the date of operation)

Desired Effect:

<1>nsdate-You need to convert dates to the same format by NSDateFormatter (date format Class) to each other, and the computed interval is presented in seconds.

<2>nscalendar (Calendar Class)

    • --Use the current calendar object to get the components in the calendar (for example, hours, minutes, minutes, etc.)
    • --You can also calculate the difference between two dates

The <3> project has extended a classification to NSDate to calculate the difference between two times, to determine whether this is the method of the year, to determine whether the method is today, and to determine whether yesterday's method

1 #import "Nsdate+chaosextension.h"2 3 @implementationnsdate (chaosextension)4 5-(Nsdatecomponents *) Deltafromdate: (NSDate *) from6 {7     //the current Calendar object8Nscalendar *calendar =[Nscalendar Currentcalendar];9     //the unit to getTenNscalendarunit unit = Nscalendarunityear | Nscalendarunitmonth | Nscalendarunitday | Nscalendarunithour | Nscalendarunitminute |Nscalendarunitsecond; One      A     //Gets the date component that calculates the travel value -nsdatecomponents *cmpt = [Calendar components:unit fromdate:from todate:self options:0]; -     returncmpt; the } - //whether this year --(BOOL) isthisyear - { +Nscalendar *calendar =[Nscalendar Currentcalendar]; -Nsinteger nowyear =[Calendar component:nscalendarunityear fromdate:[nsdate Date]]; +Nsinteger selfyear =[Calendar Component:nscalendarunityear fromdate:self]; A     returnNowyear = =selfyear; at } - //whether it is today, the month and day are to compare --(BOOL) IsToday - { -Nscalendar *calendar =[Nscalendar Currentcalendar]; -Nscalendarunit unit = Nscalendarunityear | Nscalendarunitmonth |Nscalendarunitday; inNsdatecomponents *nowcmpt =[Calendar components:unit fromdate:[nsdate Date]]; -Nsdatecomponents *selfcmpt =[Calendar Components:unit fromdate:self]; to     returnNowcmpt.year = =Selfcmpt.year +&& Nowcmpt.month = =Selfcmpt.month -&& Nowcmpt.day = =Selfcmpt.day; the } *  $-(BOOL) IsyesterdayPanax Notoginseng { -Nscalendar *calendar =[Nscalendar Currentcalendar]; theNscalendarunit unit = Nscalendarunityear | Nscalendarunitmonth |Nscalendarunitday; +Nsdatecomponents *nowcmpt =[Calendar components:unit fromdate:[nsdate Date]]; ANsdatecomponents *selfcmpt =[Calendar Components:unit fromdate:self]; the     returnNowcmpt.year = =Selfcmpt.year +&& Nowcmpt.month = =Selfcmpt.month -&& Nowcmpt.day-selfcmpt.day = =1; $ } $  - @end
    • Overrides the creation time attribute in the model
-(NSString *) create_time{NSString*timestr =Nil; NSDateFormatter*FMT =[[NSDateFormatter alloc] init]; Fmt.dateformat=@"YYYY-MM-DD HH:mm:ss"; NSDate*date =[FMT Datefromstring:_create_time]; if(date.isthisyear) {//this year        if(Date.istoday) {//Todaynsdatecomponents*cmpt =[[NSDate Date] deltafromdate:date]; Chaoslog (@"%zd%zd%zd", Cmpt.hour,cmpt.minute,cmpt.second); if(Cmpt.hour >1) {//time interval > one hourTIMESTR = [NSString stringWithFormat:@"%zd hours ago", Cmpt.hour]; } Else if(Cmpt.minute >1) {//one hour > time interval > One minuteTIMESTR = [NSString stringWithFormat:@"%zd minutes ago", Cmpt.minute]; } Else{//One minute > time intervalTimestr =@"just"; }        } Else if(Date.isyesterday) {//YesterdayFmt.dateformat =@"Yesterday HH:mm:ss"; Timestr=[FMT stringfromdate:date]; } Else{//otherFmt.dateformat =@"MM-DD HH:mm:ss"; Timestr=[FMT stringfromdate:date]; }    } Else{//not this yearTimestr =_create_time; }    returntimestr;}

Three. Refactoring the sub-controller-video posts in the project, audio posts, picture posts, satin posts, etc., the implementation of the code is almost the same, a different place is the type of request to the server. Simplified method:

<1> inherit, extract a parent class

<2> directly with a class, adding a ' type ' attribute to the class, using the ' type ' of the corresponding sub-controller when requesting data from the server (benefits, reduced n classes)

Four. Calculate the height of the cell (emphasis is on calculating the height of the text based on the width of the text and text display)

    • Bad practice--Calculate the cell height in the proxy method, the disadvantage is: every time to calculate
    • Recommended Practice:--each model corresponds to a cell, each cell has its own height, so the Cellheight property is extended in the model to store the height of its own model cell
1 //the height of the cell2-(cgfloat) cellheight3 {4     if(!_cellheight) {5         6         //the Y value of the text box7CGFloat labely = Chaoscellheadimageh +2*Chaosmargin;8         //width of the text box9CGFloat LABELW = [UIScreen mainscreen].bounds.size.width-4*Chaosmargin;Ten          OneCgsize size =Cgsizemake (LABELW, Cgfloat_max); A         //the height of the text box--the key options to use the -CGFloat labelh = [_text boundingrectwithsize:size options:nsstringdrawinguseslinefragmentorigin attributes:@{ Nsfontattributename: [Uifont systemfontofsize:14.0]} context:nil].size.height; -         //Cell Height the_cellheight = labely + labelh + Chaoscellbottombarh +2*Chaosmargin; -          -         if(_type = = chaostopictypepicture) {//Image -              +             //Calculate the width of the picture display -CGFloat picturew =Labelw; +             //Calculate the height of the picture display ACGFloat Pictureh = picturew * Self.height/Self.width; at              -             //determine if the picture is too long -             if(Pictureh >=chaoscellpicturemaxh) { -Self.bigpicture =YES; -                 //Limit Height -Pictureh =Chaoscellpictureovermaxh; in}Else { -Self.bigpicture =NO; to             } +              -             //Calculate the Y value of a picture theCGFloat Picturey = labely + Labelh +Chaosmargin; *_pictureframe =CGRectMake (Chaosmargin, Picturey, Picturew, Pictureh); $_cellheight = labely + labelh + Chaoscellbottombarh +2* Chaosmargin + Pictureh +Chaosmargin;Panax Notoginseng         } -     } the     return_cellheight; +}

Five. Display of image posts

    

    • When calculating the height of the cell, the height of the picture is calculated. And calculate the frame of the picture, extend a Pictureframe property to the model to store the frame of the picture
    • Cell to determine the type, create the corresponding xib, and add, here is the picture, is to use Xib to describe the

iOS no sister project-pull down on the Essence module notes, date display, refactoring sub-controller, calculate cell height (only once), Picture Post display

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.