您好,登錄后才能下訂單哦!
類(lèi)和文件
AppDelegate.m
#import "AppDelegate.h" #import "MainViewController.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; MainViewController *mainVC = [[MainViewController alloc] init]; UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:mainVC]; self.window.rootViewController = navVC; [mainVC release]; [navVC release]; [_window release]; return YES; } - (void)dealloc { [_window release]; [super dealloc]; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
MainViewController.h
#import <UIKit/UIKit.h> @interface MainViewController : UIViewController @property (nonatomic , retain)NSMutableArray *array; @end
MainViewController.m
#import "MainViewController.h" @interface MainViewController ()<UITableViewDataSource , UITableViewDelegate> @property (nonatomic , retain)UITableView *tableView; //屬性 @end @implementation MainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization self.array = [NSMutableArray arrayWithObjects:@"鄭浩", @"吳月", @"楊雪", @"彭宇峰", @"高軍全", @"胡寒予", @"陳騰飛", @"趙相慶", @"于青池", @"任慶民", @"謝菊花", @"呂俊廷", @"黃舜", @"翟英鵬", @"孟兆旭", @"王棟", @"卞成龍", @"張佳美", @"趙麟嶸", @"南國(guó)林", @"王俊", @"劉福彧", @"劉亞芳", nil]; } return self; } - (void)dealloc { [_tableView release]; [_array release]; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.title = @"通訊錄"; self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain]; self.tableView.dataSource = self; self.tableView.delegate = self; [self.view addSubview:self.tableView ]; [self.tableView release]; //開(kāi)啟tableView的編輯模式 // [tableView setEditing:YES animated:YES]; //系統(tǒng)提供的一個(gè)編輯按鈕 self.navigationItem.rightBarButtonItem = self.editButtonItem; } //- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath //{ // //是否能夠被編輯 // if (indexPath.row == 0) { // return YES; // } // return NO; //} - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { //1.獲取到要移動(dòng)的數(shù)據(jù)源 NSString *str = [[self.array objectAtIndex:sourceIndexPath.row] retain]; //2.講數(shù)據(jù)從原來(lái)的位置移除掉 [self.array removeObjectAtIndex: sourceIndexPath.row]; //3.把數(shù)據(jù)放到最終的位置 [self.array insertObject:str atIndex:destinationIndexPath.row]; //4.內(nèi)存管理 [str release]; } //點(diǎn)擊編輯按鈕,系統(tǒng)會(huì)調(diào)用這個(gè)方法 - (void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; NSLog(@"edtiting:%d,animated:%d",editing,animated); //利用系統(tǒng)的編輯按鈕 改變tableView的編輯狀態(tài) [self.tableView setEditing:editing animated:animated]; } //改變cell的編輯樣式(插入/刪除) - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleInsert; } //點(diǎn)擊delete按鈕的時(shí)候,系統(tǒng)調(diào)用協(xié)議方法 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { //對(duì)進(jìn)行測(cè)操作的判斷(刪除/添加) if (UITableViewCellEditingStyleDelete == editingStyle) { //在刪除一個(gè)cell之前,一定要?jiǎng)h除數(shù)據(jù)源里面相應(yīng)的內(nèi)容 [self.array removeObjectAtIndex:indexPath.row]; //但是刪除的時(shí)候,寫(xiě)刪除相應(yīng)cell 參數(shù)1;要?jiǎng)h除的indexPath組成的數(shù)組 參數(shù)2:要?jiǎng)h除row時(shí)候展現(xiàn)動(dòng)畫(huà)效果 NSArray *array = [NSArray arrayWithObjects:indexPath, nil]; [tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationRight]; } else if (UITableViewCellEditingStyleInsert == editingStyle){ NSString *name = @"ssss"; [self.array addObject:name]; NSArray *array = [NSArray arrayWithObjects:indexPath, nil]; [tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationLeft]; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.array count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *str = @"aaa"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease]; } cell.textLabel.text = [self.array objectAtIndex:indexPath.row]; cell.detailTextLabel.text = @"全班人名"; return cell; } //- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath //{ // NSLog(@"%@", [self.array objectAtIndex:indexPath.row]); //} //- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView //{ // return 5; //} //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section //{ // return [NSString stringWithFormat:@"section:%d",section]; //} //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section //{ // return 25; //} - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。