1: Convert custom objects into NSData database
To convert to NSData custom object to follow <NSCoding>protocol, and then implement Encodewithcoder,initwithcode to attribute conversions, with the following example: Hmshop.h#import<Foundation/Foundation.h>@interface Hmshop:nsobject<NSCoding>@property (nonatomic, copy) NSString*Name: @property (nonatomic, assign)DoublePrice ; @endHMShop. M#import"HMShop.h"@implementation Hmshop- (void) Encodewithcoder: (Nscoder *) encoder{[encoder encodeObject:self.name Forkey:@"name"]; [Encoder encodeDouble:self.price Forkey:@" Price"];}-(ID) Initwithcoder: (Nscoder *) decoder{if(self =[Super Init]) {Self.name= [Decoder Decodeobjectforkey:@"name"]; Self.price= [Decoder Decodedoubleforkey:@" Price"]; } returnSelf ;}-(NSString *) description{return[NSString stringWithFormat:@"%@ <->%f", Self.name, Self.price];} @end Operation:- (void) addshops{Nsmutablearray*shops =[Nsmutablearray array]; for(inti =0; i< -; i++) {Hmshop*shop =[[Hmshop alloc] init]; Shop.name= [NSString stringWithFormat:@"Product--%d", I]; Shop.price= Arc4random ()%10000; NSData*data =[Nskeyedarchiver Archiveddatawithrootobject:shop]; [Self.db Executeupdatewithformat:@"INSERT into T_shop VALUES (%@);", data]; }}- (void) readshops{Fmresultset*Set= [Self.db executeQuery:@"SELECT * from T_shop LIMIT 10,10;"]; while(Set. Next) {NSData*data = [SetObjectforcolumnname:@" Shop"]; Hmshop*shop =[Nskeyedunarchiver Unarchiveobjectwithdata:data]; NSLog (@"%@", shop); }}* the object into a nsdata reason, because in the database will become a string, unfavorable conversion, so the first to convert its serialization into NSData, and then into the database, the same first for NSData re-conversion;
2: Add sub-controller, used to extract some public content layout, slimming current Viewcontroller
Detailsviewcontroller *details = [[Detailsviewcontroller alloc] init]; = Self.photo; Details. delegate = self ; [Self addchildviewcontroller:details]; = self.view.bounds; the ; = frame; [Self.view AddSubview:details.view]; [Details didmovetoparentviewcontroller:self];
3: use protocol to isolate the call
create a protocol on the child controller and then process it internally to the parameter controller. H@protocol detailsviewcontrollerdelegate- (void) Didselectphotoattributewithkey: (NSString *) key; @end @interface Detailsviewcontroller:uitableviewcontroller@property (nonatomic, strong) Photo*photo; @property (nonatomic, weak) ID<DetailsViewControllerDelegate>Delegate; @end sub-controller. M- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{NSString*key =self.keys[(Nsuinteger) Indexpath.row]; //Pass it on to the parent controller to implement the[Self.DelegateDidselectphotoattributewithkey:key]; } parent Controller. M@interface Photoviewcontroller ()<DetailsViewControllerDelegate>@end then (get the parameters, do the original book controller to do the operation):- (void) Didselectphotoattributewithkey: (NSString *) key{Detailviewcontroller*detailviewcontroller =[[Detailviewcontroller alloc] init]; Detailviewcontroller.key=key; [Self.navigationcontroller Pushviewcontroller:detailviewcontroller animated:yes];}
4: The use of KVO
//Progress Value change increased KVO value key is fractioncompleted- (void) Setprogress: (Nsprogress *) progress{if(_progress) {[_progress removeobserver:self Forkeypath:@"fractioncompleted"]; } _progress=progress; if(_progress) {[_progress addobserver:self Forkeypath:@"fractioncompleted"Options:nskeyvalueobservingoptionnew Context:nil]; }}//Message KVO Message- (void) dealloc{if(_progress) {[_progress removeobserver:self Forkeypath:@"fractioncompleted"]; } _progress=Nil;}#pragmaMark Kvo-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID)ObjectChange: (nsdictionary *) Change context: (void*) context{if([KeyPath isequaltostring:@"fractioncompleted"]) {nsprogress*progress = (nsprogress *)Object; Nsprogress*cellprogress =_offsourecebean.cdownloadtask.progress; BOOL belongself=NO; if(cellprogress && cellprogress = =progress) {belongself=YES; } dispatch_async (Dispatch_get_main_queue (),^{ if(self) {[self showprogress:progress belongself:belongself]; } }); } Else{[Super Observevalueforkeypath:keypath Ofobject:ObjectChange:change Context:context]; }}* Note that the increase in monitoring after the elimination of the need to eliminate, remove the observation, where addobserver can be other objects, and then implement the Observevalueforkeypath in its internal protocol; You can set the options type when listening, or you can add multiple types together ; For example Nskeyvalueobservingoptionnew| Nskeyvalueobservingoptionold; When the object being monitored changes, the listener is notified immediately so that it can make some response, such as the update of the view;
5: Custom UITableViewCell Accessoryview Determine which button is pressed
In the development of UITableView, it is often necessary to customize the Accessoryview on the right side of the cell, replace it with a button with a picture, and determine which custom button was pressed when the user taps. Create a custom button and set it to Accessoryviewif(Cell = =Nil) {Cell=[[UITableView alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:ident Ifier]; UIImage*image= [UIImage imagenamed:@"Delete.png" ]; UIButton*button =[UIButton Buttonwithtype:uibuttontypecustom]; CGRect Frame= CGRectMake (0.0,0.0, Image.size.width, image.size.height); button. Frame=frame; [Button Setbackgroundimage:image Forstate:uicontrolstatenormal]; button. BackgroundColor=[Uicolor Clearcolor]; [Button addtarget:self action: @selector (Buttonpressedaction forcontrolevents:uicontroleventtouchupinside]; Cell. Accessoryview=button;} It is also possible to add a button to the Cell.contentview. Cell.contentview Addsubview:button]; When tap is judged, the user taps the cell's Indexpath- (void) buttonpressedaction ID) Sender {UIButton*button = (UIButton *) sender; (UITableViewCell*) cell =[Button Superview]; introw =[myTable Indexpathforcell:cell].row;} For the button added to the Contentview (UITableViewCell*) cell = [[button Superview] superview];
6: Direct use of the system's own UITableViewCell, where cell.accessoryview can customize the control
#import"MyselfViewController.h"@interface Myselfviewcontroller () @property (nonatomic, retain) Nsmutablearray*DataSource, @end @implementation Myselfviewcontroller-(void) Dealloc {[_datasource release]; [Super Dealloc];}-(Nsmutablearray *) DataSource {if(!_datasource) {Self.datasource= [Nsmutablearray arraywithcontentsoffile:[[nsbundle mainbundle] Pathforresource:@"myselflist"OfType:@"plist"]]; } return_datasource;}-(instancetype) init { self=[Super initwithstyle:uitableviewstylegrouped]; if(self) {}returnSelf ;}- (void) viewdidload {[Super viewdidload]; [Self.tableview Registerclass:[uitableviewcellclass] Forcellreuseidentifier:@"Cell"]; Self.tableView.rowHeight= -; Self.navigationItem.title=@"my own";}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}#pragmaMark-table View Data source-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {returnSelf.datasource.count;}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {//Return The number of the rows in the section. return[Self.datasource[section] count];}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {UITableViewCell*cell = [TableView dequeuereusablecellwithidentifier:@"Cell"Forindexpath:indexpath]; Nsdictionary*dict =[self.datasource[indexpath.section] objectAtIndex:indexPath.row]; Cell.textLabel.text= dict[@"title"]; Cell.imageView.image= [UIImage imagenamed:dict[@"ImageName"]]; if(Indexpath.section = =2&& Indexpath.row = =0) {Cell.accessoryview=[[[ Uiswitch alloc] init] autorelease]; } Else{Cell.accessorytype=Uitableviewcellaccessorydisclosureindicator; } returnCell;} @end
iOS Development Basics-Fragmentation 15