您好,登錄后才能下訂單哦!
類和文件
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:@"鄭浩", @"吳月", @"楊雪", @"彭宇峰", @"高軍全", @"胡寒予", @"陳騰飛", @"趙相慶", @"于青池", @"任慶民", @"謝菊花", @"呂俊廷", @"黃舜", @"翟英鵬", @"孟兆旭", @"王棟", @"卞成龍", @"張佳美", @"趙麟嶸", @"南國林", @"王俊", @"劉福彧", @"劉亞芳", 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]; //開啟tableView的編輯模式 // [tableView setEditing:YES animated:YES]; //系統提供的一個編輯按鈕 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.獲取到要移動的數據源 NSString *str = [[self.array objectAtIndex:sourceIndexPath.row] retain]; //2.講數據從原來的位置移除掉 [self.array removeObjectAtIndex: sourceIndexPath.row]; //3.把數據放到最終的位置 [self.array insertObject:str atIndex:destinationIndexPath.row]; //4.內存管理 [str release]; } //點擊編輯按鈕,系統會調用這個方法 - (void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; NSLog(@"edtiting:%d,animated:%d",editing,animated); //利用系統的編輯按鈕 改變tableView的編輯狀態 [self.tableView setEditing:editing animated:animated]; } //改變cell的編輯樣式(插入/刪除) - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleInsert; } //點擊delete按鈕的時候,系統調用協議方法 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { //對進行測操作的判斷(刪除/添加) if (UITableViewCellEditingStyleDelete == editingStyle) { //在刪除一個cell之前,一定要刪除數據源里面相應的內容 [self.array removeObjectAtIndex:indexPath.row]; //但是刪除的時候,寫刪除相應cell 參數1;要刪除的indexPath組成的數組 參數2:要刪除row時候展現動畫效果 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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。