˵ÆðÉè¼ÆÄ£Ê½£¬¸Ð¾õ×Ô¼º°ÑÎÕ²»Á˱ÊÍ·£¬ËùÒÔµ¥ÄóöiOS¿ª·¢Öеļ¸ÖÖ³£ÓÃÉè¼ÆÄ£Ê½Ì¸Ò»Ï¡£
µ¥Àýģʽ(Singleton)
¸ÅÄÕû¸öÓ¦ÓûòϵͳֻÄÜÓиÃÀàµÄÒ»¸öʵÀý
ÔÚiOS¿ª·¢ÎÒÃǾ³£Åöµ½Ö»ÐèҪijÀàÒ»¸öʵÀýµÄÇé¿ö£¬×î³£¼ûµÄιýÓÚ¶ÔÓ²¼þ²ÎÊýµÄ·ÃÎÊÀ࣬±ÈÈçUIAccelerometer.Õâ¸öÀà¿ÉÒÔ°ïÖúÎÒÃÇ»ñµÃÓ²¼þÔÚ¸÷¸ö·½ÏòÖáÉϵļÓËÙ¶È£¬µ«ÊÇÎÒÃǽö½öÐèÒªËüµÄÒ»¸öʵÀý¾Í¹»ÁË£¬Ôٶֻ࣬»áÀË·ÑÄÚ´æ¡£
ËùÒÔÆ»¹ûÌṩÁËÒ»¸öUIAccelerometerµÄʵÀý»¯·½·¨£«sharedAccelerometer,´ÓÃû×ÖÉÏÎÒÃÇÒ²ÄÜ¿´³ö´Ë·½·¨ÈÃÕû¸öÓ¦Óù²ÏíÒ»¸öUIAccelerometerʵÀý£¨PS£ºiOS
µÄ¿ª·ÅÖУ¬ÎÒÃÇÍùÍùÄÜ´Ó·½·¨ÃûÖоÍÁ˽âÕâ¸ö·½·¨µÄ×÷Óã©£¬ËüÄÚ²¿µÄÈçºÎʵÏÖÎÒÃÇÔÝÇÒ²»Ì¸£¬ÏÈÀ´¿´¿´»¹ÓÐÄÄЩÀàͬÑùʹÓÃÁ˵¥Àýģʽ¡£
UIApplicationÀàÌṩÁË £«sharedAPplication·½·¨´´½¨ºÍ»ñÈ¡UIApplicationµ¥Àý
NSBundleÀàÌṩÁË +mainBunle·½·¨»ñÈ¡NSBundleµ¥Àý
NSFileManagerÀàÌṩÁË £«defaultManager·½·¨´´½¨ºÍ»ñµÃNSFileManagerµ¥Àý¡££¨PS£ºÓÐЩʱºòÎÒÃÇµÃ·ÅÆúʹÓõ¥Àýģʽ£¬Ê¹Óãinit·½·¨È¥ÊµÏÖÒ»¸öеÄʵÀý£¬±ÈÈçʹÓÃίÍÐʱ£©
NSNotificationCenterÌṩÁË £«defaultCenter·½·¨´´½¨ºÍ»ñÈ¡NSNotificationCenterµ¥Àý£¨PS£º¸ÃÀ໹×ñÑÁËÁíÒ»¸öÖØÒªµÄÉè¼ÆÄ£Ê½£º¹Û²ìÕßģʽ£©
NSUserDefaultsÀàÌṩÁË £«defaultUserDefaults·½·¨È¥´´½¨ºÍ»ñÈ¡NSUserDefaultsµ¥Àý
µÈµÈ£¬Æ»¹ûµÄSDKÖдóÁ¿µÄ×ñÑ´ËÉè¼ÆÄ£Ê½£¬ÄÇôËüµÄÄÚ²¿ÊÇÈçºÎʵÏÖµÄÄØ£¿
Ê×Ïȸø´ó¼Ò½éÉÜÒ»ÏÂGCD¼¼Êõ£¬ÊÇÆ»¹ûÕë¶ÔÓÚ¶àºËCPUµÄ¶àÈÎÎñ½â¾ö·½°¸¡£Äã²»ÐèÒªÁ˽â¸ü¶à£¬Ö»ÐèÒªÖªµÀÊÇÒ»×é»ùÓÚCÓïÑÔ¿ª·¢µÄAPI£¨ÏêϸÄÚÈÝ¿ÉÒÔ¿´Ò»ÏÂÌÆÇÉǰ±²µÄÕâÆª²©ÎÄ£ºhttp://blog.devtang.com/blog/2012/02/22/use-gcd/
£©¡£GCDÌṩÁËÒ»¸ödispatch_onceº¯Êý£¬Õâ¸öº¯ÊýµÄ×÷ÓþÍÊDZ£Ö¤block£¨´úÂë¿é£ºÔÝʱÀí½âΪһ¸ö¸úº¯ÊýÏà½üµÄ¶«Î÷£¬¾ßÌå¿ÉÒÔ²ÎÕÕÕâÆªÎÄÕ£ºhttp://blog.csdn.net/enuola/article/details/8674063
£©ÀïµÄÓï¾äÔÚÕû¸öÓ¦ÓõÄÉúÃüÖÜÆÚÀïÖ»Ö´ÐÐÒ»´Î¡£
OK£¬½ÓÏÂÀ´¾Í¸ø³öÒ»¸öʹÓÃÁ˵¥Àýģʽн¨ºÍ»ñȡʵÀýµÄÀàÄ£°æ£¬´úÂëÈçÏ£º
//Singleton.h @interface Singleton : NSObject + (Singleton *)sharedSingleton; <1> @end /***************************************************************/ //Singleton.m #import "Singleton.h" @implementation Singleton static Singleton *sharedSingleton = nil;<2> + (Singleton *)sharedSingleton{ static dispatch_once_t once;<3> dispatch_once(&once,^{ sharedSingleton = [[self alloc] init];<4> //dosometing }); return sharedSingleton;<5> } |
ÉÏÊö´úÂëÖÐÓÐ5С²½£¬½âÊÍÈçÏ£º
1. ÉùÃ÷Ò»¸ö¿ÉÒÔн¨ºÍ»ñÈ¡µ¥¸öʵÀý¶ÔÏóµÄ·½·¨
2. ÉùÃ÷Ò»¸östaticÀàÐ͵ÄÀà±äÁ¿
3. ÉùÃ÷Ò»¸öÖ»Ö´ÐÐÒ»´ÎµÄÈÎÎñ
4. µ÷ÓÃdispatch_onceÖ´ÐиÃÈÎÎñÖ¸¶¨µÄ´úÂë¿é£¬ÔڸôúÂë¿éÖÐʵÀý»¯ÉÏÎÄÉùÃ÷µÄÀà±äÁ¿
5. ·µ»ØÔÚÕû¸öÓ¦ÓõÄÉúÃüÖÜÆÚÖÐÖ»»á±»ÊµÀý»¯Ò»´ÎµÄ±äÁ¿
OK£¬Õâ¾ÍÊÇiOS¿ª·¢Öе¥ÀýģʽµÄ»úÖÆ£¬ÏÂÃæÎÒÃǾͿ´¿´ÈçºÎÔÚʵ¼Ê¿ª·¢ÖÐʹÓôËģʽ£¿£¨PS£ºÎªÁ˾¡¿ÉÄܵÄÍ»³öºËÐÄÄÚÈÝ£¬ÎÒÃÇ»á¶ÔÉè¼ÆÖÐµÄÆäËûģʽ»òÄÚÈÝÒ»±Ê´ø¹ý£©
¼ÙÈçÎÒÃÇÐèÒªÔÚiOSÓ¦ÓÃÖÐʵÏÖ·Ö²ãµÄ¼Ü¹¹Éè¼Æ£¬¼´ÎÒÃÇÐèÒª°ÑÊý¾ÝµÄ³Ö¾Ã²ã£¬Õ¹Ê¾²ã£¬ºÍÂß¼²ã·Ö¿ª¡£ÎªÁËÍ»³öÖØµã£¬ÎÒÃÇÖ±½Ó°ÑÄ¿¹âתµ½³Ö¾Ã²ã£¬¶ø¸ù¾ÝMVCµÄÉè¼ÆÄ£Ê½£¬ÎÒÃÇÓÖ¿ÉÒ԰ѳ־òãϸ·ÖΪDAO²ã£¨·ÅÖ÷ÃÎÊÊý¾Ý¶ÔÏóµÄËÄÀà·½·¨£©ºÍDomain²ã£¨¸÷ÖÖʵÌåÀ࣬±ÈÈçѧÉú£©£¬ÕâÑù¾Í¿ÉÒÔʹÓÃDAO²ãÖеķ½·¨£¬ÅäºÏʵÌåÀàDomain²ã¶ÔÊý¾Ý½øÐÐÇåÎúµÄÔöɾ¸Ä²é¡£ÄÇôÎÒÃÇÈçºÎÉè¼ÆÄØ£¿
´ÓʹÓÃÕߵĽǶȿ´£¬ÎÒÃÇÆÚÍû»ñµÃDAO²ãµÄÀàʵÀý£¬È»ºóµ÷ÓÃËüµÄÔöɾ¸Ä²éËÄ´ó·½·¨¡£¿ÉÊÇÕâ¸öÀàʵÀý£¬ÎÒÃÇËÆºõÖ»ÐèÒªÒ»¸ö¾Í×ã¹»ÁË£¬ÔÙ¶àµÄ»°²»ÀûÓÚ¹ÜÀíÇÒÀË·ÑÄÚ´æ¡£OK£¬ÎÒÃÇ¿ÉÒÔʹÓõ¥ÀýģʽÁË£¬´úÂëÈçÏ£º
.hÎļþ£º
//StudentDAO.h @interface StudentDAO:NSObject @property (nonatomic,strong) NSMutaleArray *StudentsInfo; + (StudentDAO *)sharedStudentDAO; -(int) create:(Student*)student; -(int) remove:(Student*)student; -(int) modify:(Student*)student; -(NSMutaleArray) findAll; @end |
.mÎļþ£º
//StudentDAO.m #import "StudentDAO.h" #import "Student.h" @implementation StudentDAO static StudentDAO *studentDao = nil; + (StudentDAO)sharedStudentDAO{ static dispatch_once_t once; dispatch_once(&once,^{ Student *student1 = [[Student alloc]init]; student1.name = "MexiQQ"; student1.studentNum = "201200301101"; Student *student2 = [[Student alloc]init]; student2.name = "Ricardo_LI"; student2.studentNum = "201200301102"; studentDao = [[self alloc] init]; studentDao._StudentsInfo = [[NSMutaleArray alloc]init]; [studentDao._StudentsInfo addObject:student1]; [studentDao._StudentsInfo addObject:student2]; }); return studentDao; } //²åÈëµÄ·½·¨ -(int)create:(Student*)stu{ [self._StudentsInfo addObject:stu]; return 0; } //ɾ³ýµÄ·½·¨ -(int)remove:(Student*)stu{ for(Student* s in self._StudentsInfo){ if([stu.studentNum isEqual:s.studentNum]){ [self._StudentsInfo removeObject:s] break; } } } -(int)modify...... //Ê¡ÂÔ²»Ð´ -(NSMutaleArray)findAll...... //Ê¡ÂÔ²»Ð´ |
ÉÏÊöÀý×Ó²»ÄÑÀí½â£¬ÆäÖÐÓõ½µÄStudentÀàÎÒÕâÀï¾Í²»¸ø³öÁË£¬Ö»ÊÇÒ»¸öº¬ÓÐÐÕÃûºÍѧºÅÊôÐÔµÄʵÌåÀà¡£
¹Û²ìÕßģʽ
¸ÅÄһ¸ö¶ÔÏó״̬¸Ä±ä£¬Í¨ÖªÕýÔÚ¶ÔËû½øÐй۲ìµÄ¶ÔÏó£¬ÕâЩ¶ÔÏó¸ù¾Ý¸÷×ÔÒªÇó×ö³öÏàÓ¦µÄ¸Ä±ä
ͼÀý£º

ÈçͼËùʾ£º²Ù×÷¶ÔÏóÏò±»¹Û²ìÕß¶ÔÏóͶËÍÏûÏ¢£¬Ê¹µÃ±»¹Û²ìÕßµÄ״̬µÃÒԸı䣬ÔÚ´Ë֮ǰÒѾÓй۲ìÕßÏò±»¹Û²ì¶ÔÏó×¢²á£¬¶©ÔÄËüµÄ¹ã²¥£¬ÏÖÔÚ±»¹Û²ì¶ÔÏó½«×Ô¼º×´Ì¬·¢Éú¸Ä±äµÄÏûÏ¢¹ã²¥³öÀ´£¬¹Û²ìÕß½ÓÊÕµ½ÏûÏ¢¸÷×Ô×ö³öÓ¦±ä¡£
OK£¬ÎÒÃÇÏÈÀ´¿´¿´ÔÚÆ»¹ûµÄCocoa Touch¿ò¼ÜÖÐÓÐËʹÓÃÁ˹۲ìÕßģʽ£º
֪ͨ(notification)»úÖÆ
ÔÀíͼÈçÏ£º

ÈçͼËùʾ£¬ÔÚ֪ͨ»úÖÆÖжÔij¸ö֪ͨ¸ÐÐËȤµÄËùÓжÔÏ󶼿ÉÒÔ³ÉΪ½ÓÊÜÕß¡£Ê×ÏÈ£¬ÕâЩ¶ÔÏóÐèÒªÏò֪ͨÖÐÐÄ£¨NSNotificationCenter£©·¢³öaddObserver:selector:name:object:ÏûÏ¢½øÐÐ×¢²á£¬ÔÚͶËͶÔÏóͶËÍ֪ͨË͸øÍ¨ÖªÖÐÐÄʱ£¬Í¨ÖªÖÐÐľͻá°Ñ֪ͨ¹ã²¥¸ø×¢²á¹ýµÄ½ÓÊÜÕß¡£ËùÓеĽÓÊÜÕß²»ÖªµÀ֪ͨÊÇËͶË͵쬲»È¥¹ØÐÄËüµÄϸ½Ú¡£Í¶ËͶÔÏóºÍ½ÓÊÜÕßÊÇÒ»¶Ô¶àµÄ¹ØÏµ¡£½ÓÊÜÕßÈç¹û¶Ô֪ͨ²»ÔÙ¹Ø×¢£¬»á¸øÍ¨ÖªÖÐÐÄ·¢ËÍremoveObserver:name:Object:ÏûÏ¢½â³ý×¢²á£¬ÒÔºó²»ÔÙ½ÓÊÜ֪ͨ¡£
£¨ps:Õâ¶Î»°ÄÚÈÝÕª³×Թض«ÉýÏÈÉúµÄÎÄÕ£©
OK£¬ÎÒÃÇÊÔ×ÅȥʹÓÃÒ»ÏÂ֪ͨ»úÖÆ£º
н¨Ò»¸öSingle view Project,¶ÔÏîÄ¿ÖеÄÎļþ×öÒÔÏÂÐ޸ģº
AppDelegate.m
- (void)applicationDidEnterBackground:(UIApplication *)application { [[NSNotificationCenter defaultCenter]postNotificationName:@"APPTerminate" object:self]; } |
ViewController.m
// // ViewController.m // TestNotification // // Created by liwenqian on 14-10-18. // Copyright (c) 2014Äê liwenqian. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //×¢Òâ´Ë´¦µÄselectorÓвÎÊý£¬Òª¼ÓðºÅ [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(doSomething:) name:@"APPTerminate" object:nil]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; [[NSNotificationCenter defaultCenter]removeObserver:self]; // Dispose of any resources that can be recreated. } #pragma mark -´¦Àí֪ͨ -(void)doSomething:(NSNotification*)notification{ NSLog(@"ÊÕµ½Í¨Öª"); } @end |
ÈçÉÏËùʾ£¬¶ÔÄ£°æÏîÄ¿µÄÁ½¸öÎļþµÄ·½·¨»òÕû¸öÎļþ×ö³öÐ޸ģ¬Command+RÔËÐÐÄãµÄAPP£¬ÔÙ°´ÏÂHome¼ü£¨Command+H£©£¬»á·¢ÏÖ´òÓ¡³öÒ»ÐÐÊÕµ½Í¨ÖªµÄÎÄ×Ö£¬ÈçÏ£º

ÔÚAPPÍ˵½ºǫ́ʱ£¬·¢³ö¹ã²¥£¬¶øviewControllerÒòΪʱ¹Û²ìÕߣ¬ÊÕµ½¹ã²¥£¬Ö´ÐÐdoSomething·½·¨£¬´òÓ¡³öÊÕµ½¹ã²¥µÄÎÄ×Ö¡£
KVO(Key-Value-Observing)»úÖÆ
ÔÀíͼÈçÏ£º

ÈçͼËùʾ£º
¸Ã»úÖÆÏ¹۲ìÕßµÄ×¢²áÊÇÔÚ±»¹Û²ìÕßµÄÄÚ²¿½øÐе쬲»Í¬ÓÚ֪ͨ»úÖÆ£¨Óɹ۲ìÕß×Ô¼º×¢²á£©£¬ÐèÒª±»¹Û²ìÕߺ͹۲ìÕßͬʱʵÏÖÒ»¸öÐÒ飺NSKeyValueObserving£¬±»¹Û²ìÕßͨ¹ýaddObserver:forKeypath:options:context·½·¨×¢²á¹Û²ìÕߣ¬ÒÔ¼°Òª±»¹Û²ìµÄÊôÐÔ¡£
н¨Ò»¸ösingle view project£¬Í¬Ê±Ð½¨Ò»¸ö¼Ì³Ð×ÔNSObjectµÄTestWatcheÀࣺÎļþ½á¹¹ÈçÏÂͼ£º

¶ÔÎļþ½øÐÐÈçÏÂÐ޸ģº
AppDelegate.h
// // AppDelegate.h // TestNotification // // Created by liwenqian on 14-10-18. // Copyright (c) 2014Äê liwenqian. All rights reserved. // #import <UIKit/UIKit.h> #import <CoreData/CoreData.h> #import "TestWatche.h" @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator; @property (strong,nonatomic) NSString *state; @property (strong,nonatomic) TestWatche *watcher; - (void)saveContext; - (NSURL *)applicationDocumentsDirectory; @end |
AppDelegate.m ¶ÔÈçÏ·½·¨×ö³öÐÞ¸Ä
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)
launchOptions { // Override point for customization after application launch. self.watcher = [TestWatche alloc]; [self addObserver:self.watcher forKeyPath:@"state" options:NSKeyValueObservingOptionOld |
NSKeyValueObservingOptionNew context:@"pass content"]; self.state = @"launch"; return YES; } - (void)applicationDidEnterBackground:(UIApplication *)application { self.state = @"backgroud"; } |
TestWatche.m£¨ÓÉÓڼ̳Ð×ÔNSObject,¶øNSObjectÒÑʵÏÖÁËNSKeyValueObservingÐÒ飬ËùÒÔ²»ÐèÒª×öÉùÃ÷£©
// // TestWatche.m // TestNotification // // Created by liwenqian on 14-10-18. // Copyright (c) 2014Äê liwenqian. All rights reserved. // #import "TestWatche.h" @implementation TestWatche -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ NSLog(@"state change:%@",change); } @end |
OK£¬Command+B Command+R Command+H¿´¿´ÄãµÄÓ¦ÓÃÊä³öÁËʲô£¬Èç¹ûÄãµÄ²Ù×÷ÎÞÎóµÄ»°£¬»áÏÔʾÈçÏÂÄÚÈÝ£º

ίÍÐģʽ
¸öÈËÈÏΪίÍÐģʽ´ó¶àÊýÈ˽âÊ͵ĸ´ÔÓÁË£¬Æäʵ¾ÍÏñÊÇjavaÖеĽӿڣ¬Àà¿ÉÒÔʵÏÖ»ò²»ÊµÏÖÐÒ飨½Ó¿Ú£©Öеķ½·¨¡£Í¨¹ý´ËÖÖ·½Ê½£¬´ïµ½×î´óµÄ½âñîÄ¿µÄ£¬·½±ãÏîÄ¿µÄÀ©Õ¹¡£²»¹ýÄãÐèÒªÉèÖÃÓ¦ÓõÄίÍжÔÏó£¬ÒÔÈ·¶¨ÐÒéÖеķ½·¨ÎªË·þÎñ¡£
ÄÃ×î³£ÓõÄUITableViewDelegate UITableViewDataSourceÀ´¾ÙÀý£º
ʵÏÖÒ»¸öÒ³ÃæÓÐÒ»¸öUItableView£¬UItableViewµÄÿһÀ¸£¨cell£©µÄÊý¾ÝÓÉÎÒÃÇÖ¸¶¨£¬ÄÇôÎÒÃǸÃÈçºÎ×öÄØ£¿Æ»¹ûÒ²×ÔÈ»Ïëµ½ÁËÕâÒ»µã£¬ÓÚÊǶ¨ÒåÁËÒ»¸ö½Ó¿Ú£¬Õâ¸ö½Ó¿ÚÓÐÐí¶àµÄ·½·¨£¬Ö»ÐèÒªÎÒÃǰÑÒª·þÎñµÄ¶ÔÏó´«½øÈ¥£¬¾Í¿ÉÒÔʹÓÃÕâЩ·½·¨ÁË£¬Õâ¸ö½Ó¿Ú¾ÍÊÇίÍкÍÐÒé¡£¶øUITableViewDelegate
ºÍ UITableViewDataSource ¾ÍÊÇרΪUITableView¶øÐ´µÄίÍкÍÐÒé¡£Ó÷¨ÈçÏ£º
ViewController.h
// // ViewController.h // RecipeBookMe // // Created by liwenqian on 14-9-10. // Copyright (c) 2014Äê liwenqian. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> @property (nonatomic, strong) IBOutlet UITableView *mytableView; @end |
ViewController.m
// // ViewController.m // RecipeBookMe // // Created by liwenqian on 14-9-10. // Copyright (c) 2014Äê liwenqian. All rights reserved. // #import "ViewController.h" #import "DetailViewController.h" #import "Recipe.h" #import "RecipeTableCellTableViewCell.h" @interface ViewController () @end @implementation ViewController{ NSArray *recipes; } @synthesize mytableView; - (void)viewDidLoad { [super viewDidLoad]; // Initialize the recipes array Recipe *recipe1 = [Recipe new]; recipe1.name = @"Egg Benedict"; recipe1.prepTime = @"30 min"; recipe1.image = @"egg_benedict.jpg"; recipe1.ingredients = [NSArray arrayWithObjects:@"2 fresh English muffins", @"4 eggs",
@"4 rashers of back bacon", @"2 egg yolks", @"1 tbsp of lemon juice", @"125 g of butter", @"salt and pepper", nil]; Recipe *recipe2 = [Recipe new]; recipe2.name = @"Mushroom Risotto"; recipe2.prepTime = @"30 min"; recipe2.image = @"mushroom_risotto.jpg"; recipe2.ingredients = [NSArray arrayWithObjects:@"1 tbsp dried porcini mushrooms",
@"2 tbsp olive oil", @"1 onion, chopped", @"2 garlic cloves", @"350g/12oz arborio rice",
@"1.2 litres/2 pints hot vegetable stock", @"salt and pepper", @"25g/1oz butter", nil]; recipes = [NSArray arrayWithObjects:recipe1, recipe2, nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } /*--------------------------------------------------------------------*/ //¶¨ÒåUITableviewµÄÀ¸Ä¿ÊýÁ¿ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [recipes count]; } //¶¨ÒåUITableviewCellµÄÏÔʾÄÚÈÝ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CoustomerTableIdentifier = @"RecipeTableCellTableViewCell"; RecipeTableCellTableViewCell *cell =(RecipeTableCellTableViewCell *)
[tableView dequeueReusableCellWithIdentifier:CoustomerTableIdentifier]; if (cell == nil) { cell = [[RecipeTableCellTableViewCell alloc] initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:CoustomerTableIdentifier]; } recipe = [recipes objectAtIndex:indexPath.row]; cell.nameLabel.text = recipe.name; cell.prepTimeLabel.text= recipe.prepTime; cell.thumbnailImageView.image = [UIImage imageNamed:recipe.image]; return cell; } //µã»÷ÿһÀ¸Ö´ÐÐÌø×ªÊ±µÄ´¦Àí - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"showRecipeDetail"]) { NSIndexPath *indexPath = nil; Recipe *recipe = nil; indexPath = [self.mytableView indexPathForSelectedRow]; recipe = [recipes objectAtIndex:indexPath.row]; DetailViewController *destViewController = segue.destinationViewController; destViewController.recipe = [recipes objectAtIndex:indexPath.row]; } } //¶¨ÒåcellµÄ¸ß¶È - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 71; } /*--------------------------------------------------------------------*/ @end |
ÈçÉÏËùʾ£¬Á½Ìõ/------/×¢ÊͼäµÄ·½·¨È«²¿À´×ÔÓÚίÍкÍÐÒé¡£ÀûÓÃίÍкÍÐÒ飬Äã¿ÉÒÔ°ÑÖ÷Òª¾«Á¦·Åµ½Âß¼ÒµÎñÉÏ£¬½«Êý¾Ý°ó¶¨ºÍʼþ´¦Àí½»¸øÎ¯ÍкÍÐÒéÈ¥Íê³É¡£
|