1: Convert custom objects into NSData database
To convert a NSData custom object to follow the <NSCoding> protocol, and then implement the Encodewithcoder,initwithcode property conversion, the example is as follows: Hmshop.h#import <foundation/ foundation.h> @interface hmshop:nsobject <NSCoding> @property (nonatomic, copy) NSString *name; @property ( Nonatomic, assign) double price, @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:@ "Nam E "]; Self.price = [Decoder decodedoubleforkey:@ "Price"]; } return self;} -(NSString *) description{return [NSString stringwithformat:@ "%@ <->%f", Self.name, Self.price];} @end operation:-(void) addshops{nsmutablearray *shops = [Nsmutablearray array]; for (int i = 0; i<100; i++) {hmshop *shop = [[Hmshop alloc] init]; Shop.name = [NSString stringwithformat:@ "Commodity--%d", I]; Shop.price = arc4random ()% 10000; NSData *data = [Nskeyedarchiver archiveddatawithrootobject:shop]; [Self.db executeupdatewithformat:@ "INSERT into T_shop (shop) VALUES (%@);", data]; }}-(void) readshops{fmresultset *set = [self.db executequery:@ "select * from T_shop LIMIT 10, 10;"]; while (Set.next) {NSData *data = [set objectforcolumnname:@ ' 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]; Details.photo = Self.photo; Details.delegate = self; [Self addchildviewcontroller:details]; CGRect frame = self.view.bounds; FRAME.ORIGIN.Y =; Details.view.frame = 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 [Self.delegate Didselectphotoattributewithkey:key];} The 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 RE Moveobserver:self forkeypath:@ "fractioncompleted"]; } _progress = progress; if (_progress) {[_progress addobserver:self forkeypath:@ ' fractioncompleted ' options:nskeyvalueobservingoptionnew C Ontext:nil]; }}//Message KVO message-(void) dealloc{if (_progress) {[_progress removeobserver:self forkeypath:@ ' fractioncompleted ']; } _progress = nil;} #pragma mark kvo-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (Nsdictionary *) change Context: (void *) context{if ([KeyPath isequaltostring:@ "fractioncompleted"]) {nsprogress *progress = (nsprogres s *) 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:object Change: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: Accessoryview of Custom UITableViewCell
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:identifier]; 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 a tap is judged, the user taps the cell's indexpath-(void) buttonpressedaction ID) Sender { UIButton *button = (UIButton *) sender; (uitableviewcell*) cell = [Button Superview]; int row = [MyTable indexpathforcell:cell].row; For the button (uitableviewcell*) added to Contentview = [[button Superview] superview];
6: Direct use of the system comes with the 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) {} return to self;} -(void) viewdidload {[Super viewdidload]; [Self.tableview Registerclass:[uitableviewcell class] forcellreuseidentifier:@ "cell"]; Self.tableView.rowHeight = 70; Self.navigationItem.title = @ "my";} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} #pragma mark-table View data source-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView { return self.datasource.count;} -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {//Return the number of rows 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] Autorelea SE]; } else {cell.accessorytype = Uitableviewcellaccessorydisclosureindicator; } return cell; @end
iOS Development Basics-Fragmentation 15