Summary:
One of the ways iOS is stored-the list of attributes-plist, or often seen, can be conveniently used to manage some ordered batch data, especially when used to display fixed content on a form, without having to write arrays repeatedly in code, and to visualize in the Plist property list. Very handy, this article explains how to create and use a plist list and display it on a form.
To achieve the following effect:
To Create a Plist property list file:
First we create a new single view project, so we have storyboard and Viewcontroller files that we can use directly, and then we'll build a property list file.
In the list of project files on the left side of Xcode, find a location you like, usually within the supporting files folder, create a new plist file by right-clicking the new filename ... or press Command+n to open the new File window and locate the property list type file in OS X's resource:
Once created, you will get a plist file, initially as follows:
You can see that there are three columns: key, type, value, which represents the key name, types, and values, where the initial type is a dictionary, we first change the array type.
PS: See other tutorials that can change the name of the key to find the corresponding property according to the key, string type is the same, but I can not find a way to change, with the XML method is also said wrong, can only be used in the form of an array to find the location, to seek the expert pointing
For example, to ensure that the leftmost triangle arrow is down, which means that you open the root of the array under the operation, otherwise it is a sibling operation, click the plus sign, you can add data within the array, if the leftmost triangle is not down, then click the plus sign will create a new level.
Here we add three data in the array, type string, value set:
So our plist property list is set up, this visualization is very convenient, but in fact, the essence of plist is an XML file, we can see in the form of XML changes in the same way, by right-clicking on the plist file, select Open as--and Source Code, you can see it,
Change here is also possible, we still go back to the plist display way, the method is the same as just.
to add a table View in storyboard:
We open the storyboard file, in the list of controls at the bottom right, find the table view and drag it to the view in the storyboard.
Select the right-most right-hand section to associate the DataSource and Delegate of Table view with our Viewcontroller, while the table The view itself is also associated with ViewController.h to create a outlet variable so that the table view can be manipulated in the code, and the effect
The code implements the operation of the plist and displays it to the table View:1. Preparatory work
Open the. h file first, add the TableView data source and the Protocol delegate, and create a mutable array that stores the contents of the array in plist:
ViewController.h file in #import <UIKit/UIKit.h> @interface Viewcontroller:uiviewcontroller < Uitableviewdelegate, uitableviewdatasource>//Add TableView Delegate @property (Strong, nonatomic) Nsmutablearray *listData ;//used to store plist content @property (weak, nonatomic) Iboutlet UITableView *tableview; @end
2. Read Plist file
We find the file by plist the filename, save the path in the variable path, then use an array to get the contents of the Plist file and move to the variable we just created to store the plist content:
VIEWCONTROLLER.M file //Read plist nsstring *path = [[NSBundle mainbundle] pathforresource:@ "List" oftype:@ " Plist "];//find plist file nsarray *array = [[Nsarray alloc] initwithcontentsoffile:path];//get file list self.listdata = [ Nsmutablearray Arraywitharray:array]; NSLog (@ "%@", self.listdata);
3. Set the data source for table view
Here we simply provide the contents of each row of the form's row count, and we get the plist content of the corresponding position by the number of rows through the array just now:
Number of lines in VIEWCONTROLLER.M file #pragma mark TableView Data Source methods//form-(Nsinteger) TableView: (UITableView *) TableView Numberofrowsinsection: (Nsinteger) Section { return [self.listdata count];} The contents of each line of the form-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath { static NSString *simpletableidentifier = @ "Simpletableidentifier"; UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:simpletableidentifier]; if (cell = = nil) { cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier: Simpletableidentifier]; } Nsuinteger row = [Indexpath row]; Returns the plist content of the corresponding position by the number of rows cell.textLabel.text = [Self.listdata objectatindex:row]; return cell;}
4, remove the list of extra lines
Here is a simple way to directly remove unwanted form lines, in the Viewdidload method:
VIEWCONTROLLER.M file //Remove redundant table single line self.tableView.tableFooterView = [[UIView alloc]init];
the principle is not very understand, seek expert guidance
Effect
5. Add plist content
To increase the plist content, we simply add the contents to the mutable array we just stored, then write the mutable array to the original file location, and then reread the display:
VIEWCONTROLLER.M file //Add plist content [self.listdata addobject:@ "Java"]; [Self.listdata writetofile:path atomically:yes];//Write file array = nil; Array = [[Nsarray alloc] initwithcontentsoffile:path];//re -acquires NSLog (@ "%@", array); [Self.listdata removeallobjects]; [Self.listdata Addobjectsfromarray:array]; [Self.tableview reloaddata];//Reset Form
Effect
6. Delete plist content
As with the idea of adding, it is also the first operation of a mutable array, deleting the contents of a location, then storing the variable group in its original location, and then re-reading the display:
VIEWCONTROLLER.M file //delete plist content [Self.listdata removeobjectatindex:2]; [Self.listdata writetofile:path atomically:yes];//Write file array = nil; Array = [[Nsarray alloc] initwithcontentsoffile:path];//re -acquires NSLog (@ "%@", array); [Self.listdata removeallobjects]; [Self.listdata Addobjectsfromarray:array]; [Self.tableview reloaddata];//Reset Form
Effect
7. Modify Plist content
Still the same idea, modify the contents of a variable array at a location, save the original location, re-read the display:
VIEWCONTROLLER.M file //Modify plist content [self.listdata replaceobjectatindex:2 withobject:@ "Database"]; [Self.listdata writetofile:path atomically:yes];//Write file array = nil; Array = [[Nsarray alloc] initwithcontentsoffile:path];//re -acquires NSLog (@ "%@", array); [Self.listdata removeallobjects]; [Self.listdata Addobjectsfromarray:array]; [Self.tableview reloaddata];//Reset Form
Effect
The basic operation for plist is like this, it is quite convenient.
Here is my source: https://github.com/Cloudox/PlistTest
Reprint please indicate the source, thank you
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS Action Properties list plist (add and revise)