UITableViewCell的編輯,移動,添加或者刪除

來源:互聯網
上載者:User

標籤:import

類和檔案

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


本文出自 “小劉_Blog” 部落格,請務必保留此出處http://liuyafang.blog.51cto.com/8837978/1550984

UITableViewCell的編輯,移動,添加或者刪除

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.