IOS provincial two-tier data organization PHP version _ios

Source: Internet
Author: User
Tags sqlite sqlite database

The reason to publish this blog, but also from the recent development work to achieve a small demo, of course, this demo will not involve the work of some of the content of the app, below to implement the demo is universal. Because of the iteration of the project requirements, it is required to add the provincial and municipal information of the branch in the bank card binding. One of the better ways to choose this kind of information in iOS is to use Uipickerview to display it. Of course on the Pickerview on the provincial and municipal information is linked to the display, we are here because the demand is to allow users to choose provincial and municipal information, so we have two-level linkage, of course, the principle of multi-level linkage is the same. Since the old project was written using Objective-c, although now is the swift and OC mixed, but to the OC to achieve VC to add new features to be implemented with OC, so today's blog demo I do not need swift to achieve, but the principle is the same.

The screenshot below is our blog today to introduce the operating results of the demo, our blog today is to generate Pickerview data, and to the bottom of this pickerview encapsulation. It is easy to see from the animation below that the second column automatically shows all the prefecture-level cities that are saved after the first column is selected. When clicked, the corresponding label in the above will show you all the provinces and cities and the corresponding numbers. Please see below for this coarse animation.

  

One, the generation of data source (from Excel to plist)

1. A prelude to organizing data

Before encapsulating the Pickerview control, we have to have data that is not, that is, we have to have the names of provinces and cities, the corresponding codes of provinces and municipalities, and the corresponding relationship between provinces and cities. Of course, these data are on the Internet a lot, the authoritative data to see the "National Bureau of Statistics" provided by the data. Below these two screenshots is an Excel table two sheet, is our service side one program Yuan gives, is the client and the service side one standard, estimated also downloads from the net. The provincial and municipal information and coding are certainly consistent with the National Bureau of Statistics, which is beyond doubt.

How do I get this Excel form? I would like to use OC or Swift to parse the Excel table directly to read the data and then process it into the format I want. But after some understanding, the solution is rather complicated, so find another way. As a result, think of the previous use of Phpexcel this framework, because the previous time to do PHP development using Phpexcel to read Excel files. The phpexcel was pretty handy to use and it wasn't complicated, so I decided to use Phpexcel to read the data from the two sheet below.

After reading the data using Phpexcel, the data is then associated with the organization and generated JSON for use by iOS. After the iOS gets the JSON, it is parsed and stored in the plist file, so we can get the "provinces and cities" data from the Plist file, and then we can encapsulate our pickerview. Today's blog is a step-by-step to complete this thing. Of course, you can also use the SQLite database to store the data in Excel below, create two tables, a province, a release of the city, the use of foreign keys for a one-to-many relationship can be. Using the SQLite database is another solution, where we use the plist file because it's relatively simple, because the data is so small that plist files can be used on our pickerview, and if you want to use SQLite it's pretty OK, This blog value provides this solution for plist files.

2. Use Phpexecl to read provincial and municipal Excel data

The first sheet in the above Excel data stores the encoding for each province and each province, while the second sheet is the code for each city and city that is stored and gives the province of each city. Next we want to use phpexcel this third party framework to read the above Excel data, regarding Phpexcel the thing please see its official document, the address is:https://phpexcel.codeplex.com/. The code below is what we use Phpexcel to read the above Excel file, and the above data is processed to JSON encode the processed data. Below we will introduce the relevant PHP code.

(1) Loading phpexcel frame and provincial and municipal Excel files--province.xls

The PHP snippet below is loaded with the Phpexcel framework and phpexcel_iofactory to create the file reader object $objreader, and the $objreader object is advertised to load our Province.xls file. When opened, it returns a file handle object $objphpexcel that operates an Excel file, and we can manipulate the Excel file that has been opened by $objphpexcel. The specific code is shown below.

(2) Read the contents of the Excel file through the above $objphpexcel object

Next we will get the data in Province.xml by $objphpexcel the object of the operation file. The comments that are added to the main code below should be clear. The $dataarray array in the lower code fragment we use to store all the sheet data in the Province.xml. We have cycled two times to open the two sheet in this Excel, using the Setactivesheetindex () method of the $objphpexcel object to select the corresponding sheet (from left to right, 0 to N) by index. And through the object's Getactivesheet () method to get the currently selected sheet, and then return a $objworksheet object, we can read the data in each row of the current sheet by $objworksheet object.

By using foreach to iterate over each row of data in the current sheet, the same applies to foreach to iterate over the data for each column in a row. We store the data for each column in the $temprowarray array and then deposit each row of data, $temprowarray, into the $tempsheetarray of the sheet data. Finally, the current sheet data $tempsheetarray are deposited into the $dataarray. Specifically implemented as follows:

3. Verification of data

With the above steps, we can deposit data from each sheet in Excel into our array. The result of this is that the data in the $dataArray is stored in each Sheet ($tempSheetArray), Sheet ($tempSheetArray) with multiple rows ($tempRowArray), each row ($ Temprowarray) has more than one column in it. So dataarray corresponds to the entire Excel table. We also get all the Excel data. With the above code, the data for Excel is stored in $dataArray. For insurance purposes, we print the data in the $dataarray, and below is our test code.

And when we write the program, in order to reduce the amount of bugs, as well as reduce the difficulty of debugging bugs, we must develop side write code to debug the good habit. This will find bugs in time and fix, write a small function module we will test it, if there is a problem, it is easy to locate the location of the bug. The code below is a test of the above code:

  

The above code prints the data stored in $dataarray to help us see if the data stored in our $dataarray meets our expectations. The output below is the result of the test cases we have shown above, the red box above is the data in the first sheet, below is the data in the second sheet, we generally see in accordance with our expectations, it shows that our previous code is not a problem, we can Data is associated with and generates JSON data.

 

4. Correlation of provincial and municipal data

We've read the data from Excel and stored the data in a different array in the amount sheet, and then we're going to work on the data. This part is to link the data of the provinces and cities, that is to combine the data from two sheet into a block of data. Below is a chart of the data we want to store. The whole is an array, the array is a dictionary, and each dictionary represents a province. Each province's dictionary also province code code, province name name, all City citys composition. An array is stored in the citys. Each item in the array is also a dictionary, where each dictionary represents a city, and each city's dictionary includes the name of the city and the city code code. The data structure is shown below.

  

Refer to the above figure, we want to read the data processing, the data to be organized into the above structure. The code snippet below is a reorganization of the data in the Read Excel table. After the following code processing we can get the data of the above structure. The following $alldataarray store all the data information, $provinceTempData all the information stored in each province, $currentProvinceCitys store is the current province of all the city information. The IF statement in the second loop is responsible for managing the relationship between the provinces and cities, as shown in the code and code comments below.

Through the above code all of our data will be deposited into the $alldataarray, the above $alldataarray for JSON encoding and output, the bottom is processed after the output of JOSN data. Here we take Hebei province as an example. The structure in the diagram below corresponds to our data structure figure one by one above, which is exactly what we want. The task of data processing is complete half, because we have the JSON we want.

5. Parse the above JSON data and deposit it in the plist file

With these steps, PHP's work is over. The next step is to use the iOS client to access the above address to get the JSON data generated above. After we get the JSON data, we parse the JSON data and store it in the plist file in the sandbox. This allows us to load our provincial and municipal data from the plist file.

The code snippet below is the code for our iOS client, which uses Nsurlsessiondatatask to request JSON data from the provinces and cities in the file where the PHP code is located. The data is parsed after the request to the JSON data, and the JSON data is parsed into an array in the plist file stored in the sandbox through Nsfilemanager. If you want to use it externally, just find the sandbox path in the emulator and copy out the plist file. The code below is the code that the network request +json resolves the +plist file store.

After the above code is executed, you will find a file called Province.plist in the sandbox of the app in your simulator, which stores the provincial and municipal data that we want to use. The data storage structure of the plist file is the data structure we described above, and a partial screenshot of the data in the Plist file is below. At this point we have obtained a plist document of the provincial and municipal data according to our expectation.

 

Second, the packaging choice of provinces and cities of the use of Pickerview

Encapsulation, of course, is not simply a simple use of pickerview, but we need to take into account the usability and scalability of the user when encapsulating the code. Here I only do a simple package for pickerview, but there are some dry goods, mainly thinking it. Through the data organization of a large module above, we can organize the data in the Excel document given by the previous server into the plist data we want. The topic of this section is to read the data in the Plist file and display the data in the level two linkage pickerview for the user to choose. The user chooses to return to the selected provinces and cities, as well as the corresponding codes of provinces and cities. Start the encapsulation of our controls.

1. The directory structure of the encapsulated control

First of all, let's look at the overall structure of the contents of the control we encapsulate, first of all, to understand our encapsulated control. The screenshot below is the directory structure of the controls we encapsulate, because we encapsulate the uipikcerview that display the provinces and cities, so we call this provincepickerview,provincepickerview the component we're encapsulating. The user only needs to instantiate it and add it to its view to be able to use it. The province.plist data below is the data that we generated above, the data source in our Provincepickerview is province.plist file.

Provincemodel store the name and code of the currently selected provinces and cities, the second screenshot below is the content of Provincemodel. Provincecode stores the encoding of the currently selected province, Provincename stores the name of the currently selected province, Citycode stores the encoding of the selected city, and CityName stores the name of the selected city. The specific code is shown below.

2. Initialization of encapsulated controls and how they are invoked

Let's take a look at the way we encapsulate this provincepickerview, and it's easy to use. The code snippet below is Provincepickerview initialization, initializing the Provincepickerview and adding it to the view that you want to display Provincepickerview. The block callback for the Provincepickerview object is then set. The callback executes when the user clicks the completion button on the Provincepickerview and returns the model data for the city and province information selected by the current user.

The above is the initialization provincepickerview, and the data callback block is set. The code snippet below is used to display Provincepickerview, and calling the Showpickerview method pops up Provincepickerview below, because Provincepickerview itself has a Cancel button, Cancellation will automatically retract the Provincepickerview, so we do not need to provide users with the method of recovering Provincepickerview, so it is relatively simple to use.

Third, code sharing

Because the blog space is limited, as for the Provincepickerview package of code in this will not be pasted up. The very core of this is the encapsulation of the corresponding methods in the two proxies for Uipickerviewdelegate and Uipickerviewdatasource. There is how to show and hide Pickerview, in other words, is to say where Pickerview put in the display. Interested partners can come from the github to share the code from the line analysis. In detail, so do not do too much to repeat.

The code screenshot Below is part of the code that is shared on the GitHub, which can be said to have added detailed annotations, and interested small partners can read it themselves. If there is a bias in the code, it is also pointed out.

All of the above code is in the form of screenshots, which is irrelevant, at the end of the blog will give all the relevant code, of course, including the above PHP code, as well as the specific implementation of Pickerview. Well, since the blog space is limited, let's get here today. Below is the share link for this blog-related code.

GitHub Share address: Https://github.com/lizelu/ProvincePickerDemo

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.