Cell business logic processing (Time formatting), cell business

Source: Internet
Author: User
Tags dateformat

Cell business logic processing (Time formatting), cell business

1. Click More. 1. Click More for project requirements. A box is displayed at the bottom.

 

2. How to bring up a box from the bottom? Two Methods: UIActionSheet, UIAlertController 2.1, UIActionSheet, and UIAlertController. Advantages: You can directly use the disadvantages without a controller: The method has expired, and the click of the listener button is troublesome. Source Code: // If variable parameters are found later, you must add nilUIActionSheet * sheet = [[UIActionSheet alloc] initWithTitle: nil delegate: nil cancelButtonTitle: @ "cancel" destructiveButtonTitle: nil failed: @ "add to Favorites", @ "Report", nil]; [sheet showInView: self]; 2.2 advantages of using UIAlertController: The method to listen to Button clicking disadvantages: a lot of code is required, depends on the controller (requires the Controller modal out) 2.3 using U IAlertController needs to use notifications to notify the nearest controller to bring the UIAlertController modal out. Note: When Using notifications, when the controller is destroyed, the notification 2.4 must be removed. Why do you use the notification instead of the proxy? Notifications can be transferred across message proxies. Generally, messages are transmitted from the upper and lower levels. 3. How can I get a controller quickly? There is a global Singleton UIApplication. Through this Singleton, we can get the controller of the window and the Controller later as long as we want to get the Controller quickly and take the window root controller [UIApplication sharedApplication]. keyWindow. rootViewController; 2. post posting time processing 1. time processing business logic analysis 1.1 first judge whether this year's post is, if not this year, it will show 21:10:08 this format 1.2 if it is this year, when judging whether it is today, if you are judging whether there is more than one hour today, it will show how many hours ago this format is 1.3 if not more than one hour, determine whether the format is more than 2 minutes and more than two minutes. If the format 1.4 is not more than two minutes, the format 1.5 is displayed. If the time is more than one day, when judging whether there is more than one day, if not, it is displayed in the format of yesterday. 1.6 if the time exceeds one day, it is displayed in the format of 08-0. 5 21:10:08. 2. Because the business logic is very deep, it is easy to mix up when writing code. How can this problem be solved? We can first write pseudocode, clarify our ideas, and then convert it into real code. What is pseudocode: pseudocode is a pseudo-code instance written by combining Chinese characters and code // processing time (compare the post release time with the current time)
If (this year ){
If (today ){

If (greater than 1 hour ){

} Else if (greater than 2 minutes ){

} Else {// just

}

} Else if (yesterday ){

} Else {// before yesterday

}

} Else {// not this year
} 3. How to obtain the time difference? 3.1 first convert the date of the string object to the date format of the date object 3.1.1 obtain the NSDateFormatter object for NSStreing and NSDate interchange 3.1.2 set the date format (converted to what format of date) 3.1.3 convert a string date to a date in a specific format. 4. how can I determine whether it is 4.1 this year? Determine if the year is the same as 4.2. How to obtain the date year, month, and so on? You can use the calendar class to get the date component, and you can get the year, month, and hour minute and second processed Date Format original code.
1 // 1. convert the Post posting time string to (NSDate) date object => NSDateFormatter 2 // NSDateFormatter: NSStreing and NSDate convert each other to 3 NSDateFormatter * fmt = [[NSDateFormatter alloc] init]; 4 // set format 5 fmt. dateFormat = @ "yyyy-MM-dd HH: mm: ss"; 6 // release date object 7 NSDate * createDate = [fmt dateFromString: item. create_time]; 8 9 // obtain the calendar Class Object 10 // NSCalendar * calendar = [NSCalendar currentCalendar]; 11 // obtain the date component 12 // NSDateComponents * cmp = [calendar components: NSCalendarUnitHour | NSCalendarUnitMinute fromDate: createDate toDate: [NSDate date] options: NSCalendarWrapComponents]; 13 14 NSString * timeStr = item. create_time; 15 if ([createDate isThisYear]) {16 if ([createDate isThisToday]) {17 18 // get the date difference 19 if (cmp. hour> = 1) {20 timeStr = [NSString stringWithFormat: @ "% ld Hours Ago", cmp. hour]; 21} else if (cmp. minute> = 2) {22 timeStr = [NSString stringWithFormat: @ "% ld Minutes Ago", cmp. minute]; 23} else {// just 24 timeStr = @ "just"; 25} 26} else if ([createDate isThisYesterday]) {// Yesterday 27 // Yesterday fmt. dateFormat = @ "Yesterday HH: mm"; 29 timeStr = [fmt stringFromDate: createDate]; 30 31} else {// before yesterday 08-05 21: 10: 0832 fmt. dateFormat = @ "MM-dd HH: mm: ss"; 33 timeStr = [fmt stringFromDate: createDate]; 34} 35} 36 37 because date determination is frequently used, we will extract the Judgment Method to NSDate classification 38 39-(BOOL) isThisYear40 {41 // obtain the current date object 42 NSDate * curDate = [NSDate date]; 43 // obtain the calendar class 44 NSCalendar * curCalendar = [NSCalendar currentCalendar]; 45 // obtain the date component (year, month, etc) calendar class (createDate) 46 NSDateComponents * selfCmp = [curCalendar components: NSCalendarUnitYear fromDate: self]; 47 // obtain the current time and date component (year, month, etc.) 48 NSDateComponents * curCmp = [curCalendar components: NSCalendarUnitYear fromDate: curDate]; 49 return curCmp. year = selfCmp. year; 50} 51 52 // determine if it is today 53-(BOOL) isThisToday54 {55 // obtain calendar class 56 NSCalendar * curCalendar = [NSCalendar currentCalendar]; 57 return [curCalendar isdate1_day: self]; 58} 59 60-(BOOL) isThisYesterday61 {62 NSCalendar * curCalendar = [NSCalendar currentCalendar]; 63 return [curCalendar isDateInYesterday: self]; 64}

 

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.