Custom iOS selector with lunar selector code

Source: Internet
Author: User

1. In view of the different rolling effects between the simulator and the real machine, we suggest you comment out the following content in IDJScrollComponent. m during debugging:

 
 
  1. self.decelerationRate=0; 

2. In my code, the resizableImageWithCapInsets of the UIImage of iOS5.0 is used: method. If you are using the SDK below iOS5.0, replace it with stretchableImageWithLeftCapWidth: topCapHeight: method.

3. Several bugs:

(1.) I forgot to release the memory for copy.

 
 
  1. NSString *pYear=[cal.year copy]; 
  2. NSString *pMonth=[cal.month copy]; 

This pYear, pMonth is not released!

(2) There are some problems with the release of UITableViewCell.

In the cellForRowAtIndexPath method, cell initialization involves the following three situations,

 
 
  1. cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
  2. cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
  3. cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 

Assume that the cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier: CellIdentifier] is called; Initialization

Then return [cell autorelease]; The problem arises!

Change the other two types of initialization

 
 
  1. cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
  2. cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; 

Then return cell; then OK.

(3) Limitations of the algorithm.

IDJScrollComponent. moveSubViews in m: The method has a defect in algorithm, which leads to a limitation. The height of the scroll wheel image must be a number that can be divisible by the number of rows displayed. For example, in my example, three rows are displayed on the date, then, the Image Height of the scroll wheel is 177 pixels, and 177 can be divisible by 3.

(4) The-(id) initWithYearStart :( NSUInteger) start end :( NSUInteger) end of the IDJChineseCalendar. mm provided by the lunar data provider: method:

 
 
  1. // Obtain the date of the lunar calendar corresponding to the year of the current year and the year of a child
  2. Std: pair p = ChineseCalendarDB: GetEraAndYearOfLunar ([self. year intValue]);

To be moved

 
 
  1. // Set the lunar Date Field of the current time
  2. Self. year = [NSString stringWithFormat: @ "% d", chineseDate. GetYear ()];

.

This control is not a re-Implementation of the UIPickerView texture that comes with iOS. My achievements over a week have taken a lot of detours. Ha!

Reasons for developing this control:

Recently, I had a requirement to create a date wheel selector for the lunar calendar. At first I wanted to use the UIPickerView provided by iOS, but the design staff had a high requirement and must implement the style she required, that is, the style you see in. However, UIPickerView does not provide any interface for customizing styles (that is, replacing skin. First, I found a post on the Forum. According to this example, the UIPickerView image can be modified to some extent. To some extent, I have the following reasons:

(1) he uses the method of overwriting the sub-view images on the original UIPickerView. Then, if you provide new images, they must be seed together with the original ones. This also shows that even if the image is changed, the style cannot be changed. For example, the width of the split line and the size of the upper and lower sides of the border cannot be completely DIY and cannot meet my needs.

(2 .) because the method of overwriting the sub-view images on UIPickerView is not officially provided by Apple, once the implementation policy of UIPickerView in a later version is changed, for example, the sub-view number 3 is no longer the background image of the scroll wheel, your practice will go down.

So I implemented the scroll wheel selector. The difference between it and UIPickerView is:

(1) skin images provide a method for replacement.

(2) You can select whether the data on the scroll wheel is cyclically rolled.

(3) You can specify the position of the selected bar.

For code reuse, that is, the so-called OOP, I implement it in the following way (because I think the implementation scheme of this wheel selector is not a bright spot, because there are many solutions to choose from, but the structure and code reuse of the program are very important ):

(1) pickerview Directory: IDJPickerView. h In this directory is the scroll wheel selector view. There is nothing related to data, that is, you can display any data on IDJPickerView. IDJPickerView obtains the data to be displayed through delegation. The IDJScrollComponent. h In this directory provides the ability to repeatedly scroll the content on UIScrollView.

(2 .) datepicker Directory: IDJDatePickerView. h implements the protocol in IDJPickerView and uses IDJPickerView as a private member. It implements a date selector that displays the Gregorian calendar and Lunar Calendar Based on the type field, and IDJDatePickerView. the lunar calendar algorithm in m uses the C ++ algorithm in the solar_chinese_calendar directory. Therefore, the relevant data class of the lunar calendar uses the C ++ mixed code. In fact, iOS supports the lunar calendar by itself, but there is a BUG. for specific reasons, you can refer to the source code of IDJChineseCalendar. mm, which contains more detailed annotations. I also used a layer of encapsulation here, that is, IDJDatePickerView. m itself does not provide data, but uses IDJCalendar. h. IDJChineseCalendar. h. IDJCalendarUtil. h To provide, This achieves the separation of data and views (because the lunar calendar algorithm is too complex, it is directly written in IDJDatePickerView. m will make the Code look too readable), and the C ++ call is encapsulated in the data layer, so that IDJDatePickerView. m's view-Layer Code does not contain C ++ APIs.

(3) timepicker Directory: This directory is purely a Demo that shows a selector that does not scroll cyclically, and shows the asymmetry between the number of lines and the position of the selected entry. It has no actual meaning.

At first, I wrote the selector for the Chinese calendar. The code was coupled and split one by one. In this way, the code can be reused, not just the lunar selector, but can carry any data.

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.