Iarontalk Blog opens.
Not matter of the today would drag tomorrow.
-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Nsdictionary (dictionary) is used in Objective-c for "collection" data, it can be stored in the form of key-value pairs, the value of the time through key can directly get the corresponding value, very convenient. Multiple different types of data can be saved in the same Dictionary object, unlike Java and C can only hold data of the same type declared.
Well-known nsdictionary is a box of data, and "It" is a switch (switch), let's see how it plays its own characteristics into a "switch." The following is an example of the nsdictionary "switch" feature, which needs to be explained by the dictionary initialization nsdictionary *switchdic = [[Nsdictionary alloc] init]; The dictionary content is empty before setting the initial value.
Refer to this article the idea of many keywords can complete the switch function, such as nsuserdefault, compared to everyone also know each app's first use of animation (sliding view), in the second use of the app is missing, the function can be realized with this idea, see "Report".
Here is Example 1: (Application background: Comment Bar for the collection and expansion)
#import " BaseViewController.h " @interface Topdetailviewcontroller:baseviewcontroller<uitableviewdatasource,uitableviewdelegate>{ * _tableview;//table View in comment bar *_foldswitch;//definition of the unwinding switch}@end
@implementationTopdetailviewcontroller- (void) viewdidload {[Super viewdidload]; _foldswitch=[[Nsmutabledictionary alloc]init];}#pragmaMark-Select cells to expand cells-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{NSString*key = [NSString stringWithFormat:@"%d", Indexpath.row];//Use the selected cell subscript as a key in the dictionaryBOOL isshow =[[_foldswitch Objectforkey:key]boolvalue]; ;//the created dictionary is empty and Isshow is no.[_foldswitch setobject:@ (!isshow) Forkey:key];//when the cell is selected, changes the state of the _foldswitch, at which point the value of the selected cell subscript key is: @ (YES). //This line of code is especially important to refresh the selected row of cells before calling the following method. [_tableview Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade]; } #pragma-mark-table view to set cell height call method-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{//gets the currently selected cell subscript, as the key of the dictionary may correspond to the value, because in the previous protocol method we will _foldswitch in the key corresponding value is set to: @ (yes), therefore, Issshow is YESNSString *key = [NSString stringWithFormat:@"%i", Indexpath.row]; BOOL Isshow=[[_foldswitch Objectforkey:key]boolvalue]; if(Isshow = =YES) { //here to fill out the relevant code of expansion}Else{ //fill in the relevant code here }}@end
Example 2: (Application scenario: first-time use of the app, start-up animation of one-off display, later open the app, animation is not displayed)
1 #import "RootTabBarController.h"2 3 @implementationRoottabbarcontroller4 5 //to create a local storage object for small data6Nsuserdefaults *userdefaults =[Nsuserdefaults standarduserdefaults];7 //gets whether the current is the first start, and if it is first started, Firstinit is no.8BOOL firstinit = [[Userdefaults objectforkey:@"Firstinit"]boolvalue];9 if(Firstinit = =NO) {Ten[Self _initfirstview];//Load Animation method One //change the value of Firstinit to Yes, not to load the animation in the future. A[Userdefaults setobject: @YES forkey:@"Firstinit"]; - //It is recommended that you store the synchronization to disk, but not - [Userdefaults synchronize]; the}
Due to the limited level of the editor, it is unavoidable to ask the various Daniel to criticize, put forward valuable suggestions.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The "Yes" and "no" for the new use of the
ios-nsdictionary/nsmutabledictionary, switch controller, nsuserdefaults simple switch application.